繁体   English   中英

如何在实体框架中将一个实体映射到许多实体?

[英]How do I map one entity to many entities in Entity Framework?

我有4个实体。 他们看起来像这样:

public abstract class Entity
{
    public Guid Id { get; set; }
    public DateTime CreationDate { get; set; }
}

public class Client :
    Entity
{
    public string Name { get; set; }
    // ...
    public IList<Note> Notes { get; set; }
}

public class Supplier :
    Entity
{
    public string Name { get; set; }
    // ...
    public IList<Note> Notes { get; set; }
}

public class Note :
    Entity
{
    public Guid EntityId { get; set; } // This points to the Id field of a Client or Supplier
    public virtual Entity Entity { get; set; }
    public string EntityName { get; set; } // "Client", "Supplier", etc.
}

现在,我要为此创建3个表,即Client,Supplier和Note。 注释表需要指向EntityId字段上的客户和供应商注释。 实际发生的情况是,EF将“ Client_ID”和“ Supplier_ID”字段添加到“注释”表中,并对每个表添加了外键。 基本上,这样做的效果是,只有同时包含客户和供应商,才能创建便笺。

我需要怎么做才能使其表现出所需的方式?

试试这个信息

this.HasOptional(t => t.Client)
                .WithMany(t => t.Notes )
                .HasForeignKey(d => d.Id);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM