【问题标题】:Configuration of two Entites with EntityTypeConfiguration使用 EntityTypeConfiguration 配置两个实体
【发布时间】:2016-09-30 06:39:23
【问题描述】:

我有以下两个具有相应 EntityTypeConfigurations 的实体

public class Master
{
    public int Id { get; set; }
    public int RequestId { get; set; }
    public string ClientNo { get; set; }
    public string SomeValue { get; set; }
    public ICollection<Child> Childs { get; set; }
}

public class MasterConfig : EntityTypeConfiguration<Master>
{
    public MasterConfig()
    {
        ToTable("Master", "MySchema");
        HasKey(k => k.Id);
        HasMany(m => m.Childs)...
       // Connect Master.RequestId to Child.RequestId 
       //     and Master.ClientNo  to Child.ClientNo
    }
}

public class Child
{
    public int Id { get; set; }
    public int RequestId { get; set; }
    public string ClientNo { get; set; }
    public string SomeOtherValue { get; set; }
}

public class ChildConfig : EntityTypeConfiguration<Child>
{
    public ChildConfig()
    {
        ToTable("Child", "MySchema");
        HasKey(k => k.Id);
    }
}

我想这样配置它,当我这样做时

myAppContext.Masters.Include(m => m.Childs).First(m => m.Id == 4);

它将加载所有 ID 为 4 的 Master 和相应的机加工 Child。 不知怎的,我无法让它工作。

【问题讨论】:

  • 一个RequestId能有很多大师吗?
  • @AdilMammadov 是的,它可以。
  • 我想ClientNoRequestId 有关,我说的对吗?
  • 是的@AdilMammadov
  • 实际上有一个Request可以容纳更多的Master而不是容纳许多Child。数据库设计错误,Child 应该有一个 FK MasterId,但由于某些遗留原因,我将无法更改它。 @AdilMammadov

标签: c# .net entity-framework entity-framework-6


【解决方案1】:

关系基于 FKPK。在您的方案中,RequestId 不是MasterChild 的主键。你应该有Request 模型,它有RequsetId 作为PK 并且应该有Master 的导航属性,并且Child 模型不应该直接绑定到Request,它应该绑定到Master。您的模型应如下所示:

public class Request
{
    public int Id { get; set; }
    public string ClientNo { get; set; }

    public virtual ICollection<Master> MasterCollection { get; set; }
}

public class RequestMap  : EntityTypeConfiguration<Request>
{
    HasKey(m => m.Id);
}

public class Master
{
    // Id is same as RequestId
    public int Id { get; set; }
    public int RequestId { get; set; }
    public string SomeValue { get; set; }

    public virtual Request Request { get; set; }
    public virtual ICollection<Child> Childs { get; set; }
}

public class MasterConfig : EntityTypeConfiguration<Master>
{
    public MasterConfig()
    {
        ToTable("Master", "MySchema");
        HasKey(k => k.Id);

        // Map Request and Master
        HasRequired(m => m.Request)
            .WithMany(m => m.MasterCollection)
            .HasForeignKey(m => m.RequestId);
    }
}

public class Child
{
    public int Id { get; set; }
    public string SomeOtherValue { get; set; }
    public int MasterId { get; set; }

    public virtual Master Master { get; set; }
}

public class ChildConfig : EntityTypeConfiguration<Child>
{
    public ChildConfig()
    {
        ToTable("Child", "MySchema");
        HasKey(k => k.Id);

        HasRequired(m => m.Master)
            .WithMany(m => m.Childs)
            .HasForeignKey(m => m.MasterId);
    }
}

通过更改模型以适应这种情况,现在您可以根据需要加载您的 Master 和相关的 Childs

Master master = myAppContext.Masters
    .Include(m => m.Childs)
    .First(m => m.Id == 4);

如果要根据Master加载Request数据则:

Master master = myAppContext.Masters
    .Include(m => m.Request)
    .Include(m => m.Childs)
    .First(m => m.Id == 4);

您还可以加载任何孩子的MasterRequest 详细信息:

Child child = myAppContext.Childs
    .Include(m => m.Master.Request)
    .First(m => m.Id == 2);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-29
    • 2020-09-12
    • 2016-07-03
    相关资源
    最近更新 更多