gpt4 book ai didi

c# - 如何将一个实体映射到 Entity Framework 中的多个实体?

转载 作者:太空宇宙 更新时间:2023-11-03 17:00:54 25 4
gpt4 key购买 nike

我有 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 字段添加到 Note 表中,每个表都有外键。这样做的效果基本上是只有在客户和供应商都包括在内的情况下才能创建注释。

我需要做什么才能使它按照我需要的方式运行?

最佳答案

试试这个 Info

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

关于c# - 如何将一个实体映射到 Entity Framework 中的多个实体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34718540/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com