Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework core EF Core Fluent API与两个相同类型实体的映射关系_Entity Framework Core_Ef Core 2.1 - Fatal编程技术网

Entity framework core EF Core Fluent API与两个相同类型实体的映射关系

Entity framework core EF Core Fluent API与两个相同类型实体的映射关系,entity-framework-core,ef-core-2.1,Entity Framework Core,Ef Core 2.1,我一直在尝试使用fluent api为下图配置适当的映射。(如果有人将此标记为副本,出于对所有神圣事物的热爱,请包含相关链接!我已经花了好几天的时间来梳理它。) 我的主要目标是,所有实体都将有一个EnterpriseID,用作切分键 Enterprise表包含两个联系人:PrimaryContact和BillingContact 我想做的是创建一个新的企业,使用生成的代码GUIDID以及两个联系人(主联系人和账单联系人),将企业ID分配给这些联系人,并在TrackingState上调用Save

我一直在尝试使用fluent api为下图配置适当的映射。(如果有人将此标记为副本,出于对所有神圣事物的热爱,请包含相关链接!我已经花了好几天的时间来梳理它。)

我的主要目标是,所有实体都将有一个EnterpriseID,用作切分键

Enterprise表包含两个联系人:PrimaryContact和BillingContact

我想做的是创建一个新的企业,使用生成的代码GUIDID以及两个联系人(主联系人和账单联系人),将企业ID分配给这些联系人,并在TrackingState上调用SaveChanges。添加了对象层次结构(此时为企业->联系人->地址

如果没有任何流畅的映射,EF Core2.1说:“'Contact'和'Enterprise.BillingContact'之间以及'Contact'和'Enterprise.PrimaryContact'之间的关系都可以使用{'EnterpriseID}”作为外键。若要解决此问题,请在至少一个关系上显式配置外键属性。“

我尝试了许多配置,要么最终得到一个只定义了Enterprise表中的一个Contact属性的DB,要么整个混乱都会演变成FK/hell

下面是当前的类存根

公共级企业
{
公共Guid ID{get;set;}
公共联系人PrimaryContact{get;set;}
公共联系人BillingContact{get;set;}
}
公共类联系人
{
公共Guid ID{get;set;}
公共Guid EnterpriseID{get;set;}
公共字符串FName{get;set;}
公共字符串LName{get;set;}
公共广播地址{get;set;}
}
公共类商店
{
公共Guid ID{get;set;}
公共Guid EnterpriseID{get;set;}
公共联系人PrimaryContact{get;set;}
}
公共阶级秩序
{
公共Guid ID{get;set;}
公共Guid EnterpriseID{get;set;}
公共Guid存储ID{get;set;}
公共联系人CustomerContact{get;set;}
}
公共课堂演讲
{
公共Guid ID{get;set;}
公共Guid EnterpriseID{get;set;}
公共字符串行{get;set;}
}
我真的很想得到一些关于如何配置的建议

Enterprise表包含两个联系人:PrimaryContact和BillingContact

那么
企业
联系人
地址
之间的关系应该如下所示:

public class Enterprise
{
    [Key]
    public Guid ID { get; set; }

    public Guid PrimaryContactId { get; set; }
    public Contact PrimaryContact { get; set; }

    public Guid BillingContactId { get; set; }
    public Contact BillingContact { get; set; }
}

public class Contact
{
    [Key]
    public Guid ID { get; set; }
    public string FName { get; set; }
    public string LName { get; set; }

    public Address Address {get; set;}
}

public class Address
{
   [Key]
   public Guid ContactId {get; set;}
   public string Lines {get; set;}
}
然后在Fluent API配置中:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<Enterprise>().HasOne(e => e.PrimaryContact)
            .WithOne()
            .HasForeignKey<Enterprise>(e => e.PrimaryContactId).OnDelete(DeleteBehavior.Restrict);

    modelBuilder.Entity<Enterprise>().HasOne(e => e.BillingContact)
            .WithOne()
            .HasForeignKey<Enterprise>(e => e.BillingContactId).OnDelete(DeleteBehavior.Restrict);

   modelBuilder.Entity<Contact>().HasOne(c => c.Address)
            .WithOne().HasForeignKey<Address>(a => a.ContactId);
}
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
基于模型创建(modelBuilder);
modelBuilder.Entity().HasOne(e=>e.PrimaryContact)
.WithOne()
.HasForeignKey(e=>e.PrimaryContactId).OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity().HasOne(e=>e.BillingContact)
.WithOne()
.HasForeignKey(e=>e.BillingContactId).OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity().HasOne(c=>c.Address)
.WithOne().HasForeignKey(a=>a.ContactId);
}

谢谢!我想我知道我现在哪里出错了……我想把FK放在联系人而不是企业上。谢谢你的帮助。