Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.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
C# 单地址表,用于存储实体框架中多个实体的地址(使用双外键)_C#_Sql_Entity Framework_Entity Framework 6_Entity Framework Core - Fatal编程技术网

C# 单地址表,用于存储实体框架中多个实体的地址(使用双外键)

C# 单地址表,用于存储实体框架中多个实体的地址(使用双外键),c#,sql,entity-framework,entity-framework-6,entity-framework-core,C#,Sql,Entity Framework,Entity Framework 6,Entity Framework Core,我试图使用一个地址表来存储系统中具有通用键id字段的多个实体的地址。客户可以有多个地址,供应商可以有多个地址 地址类别: public int Id { get; set; } public string Name { get; set; } public string Address1 { get; set; } public string Address2 { get; set; } public string City { get; set; } public string State {

我试图使用一个
地址
表来存储系统中具有通用
键id
字段的多个实体的地址。客户可以有多个地址,供应商可以有多个地址

地址类别:

public int Id { get; set; }
public string Name { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Country { get; set; }
public string ZipCode { get; set; }
// These 2 fields make it so I can get all of the addresses for a single Customer or Vendor
public string EntityType { get; set; }
public int KeyId { get; set; }

// Navigation properties
public Customer Customer { get; set; }
public Vendor Vendor { get; set; }
public Location Location { get; set; }
public int Id { get; set; }
public string Name { get; set; }

// Navigation properties
public IList<Address> Addresses { get; set; }
public int Id { get; set; }
public string Name { get; set; }

// Navigation properties
public IList<Address> Addresses { get; set; }
客户类别:

public int Id { get; set; }
public string Name { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Country { get; set; }
public string ZipCode { get; set; }
// These 2 fields make it so I can get all of the addresses for a single Customer or Vendor
public string EntityType { get; set; }
public int KeyId { get; set; }

// Navigation properties
public Customer Customer { get; set; }
public Vendor Vendor { get; set; }
public Location Location { get; set; }
public int Id { get; set; }
public string Name { get; set; }

// Navigation properties
public IList<Address> Addresses { get; set; }
public int Id { get; set; }
public string Name { get; set; }

// Navigation properties
public IList<Address> Addresses { get; set; }
public int Id{get;set;}
公共字符串名称{get;set;}
//导航属性
公共IList地址{get;set;}
供应商类别:

public int Id { get; set; }
public string Name { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Country { get; set; }
public string ZipCode { get; set; }
// These 2 fields make it so I can get all of the addresses for a single Customer or Vendor
public string EntityType { get; set; }
public int KeyId { get; set; }

// Navigation properties
public Customer Customer { get; set; }
public Vendor Vendor { get; set; }
public Location Location { get; set; }
public int Id { get; set; }
public string Name { get; set; }

// Navigation properties
public IList<Address> Addresses { get; set; }
public int Id { get; set; }
public string Name { get; set; }

// Navigation properties
public IList<Address> Addresses { get; set; }
public int Id{get;set;}
公共字符串名称{get;set;}
//导航属性
公共IList地址{get;set;}
DbContext:

    builder.Entity<Customer>()
        .HasMany(c => c.Addresses)
        .WithOne(a => a.Customer)
        .HasForeignKey(a => a.KeyId);

    builder.Entity<Vendor>()
        .HasMany(v => v.Addresses)
        .WithOne(a => a.Vendor)
        .HasForeignKey(a => a.KeyId);
builder.Entity()
.HasMany(c=>c.address)
.WithOne(a=>a.Customer)
.HasForeignKey(a=>a.KeyId);
builder.Entity()
.HasMany(v=>v.address)
.WithOne(a=>a.供应商)
.HasForeignKey(a=>a.KeyId);
在尝试为数据库添加种子(添加供应商和几个地址)时,我遇到一个错误,错误如下:

SqlException:合并语句与外键约束“FK\U地址\U客户\U密钥ID”冲突。冲突发生在数据库“MyDatabase”、表“dbo.Customer”、列“Id”中

我很确定这是因为引用完整性,即数据库中没有具有您试图存储在
KeyId
中的Id的客户

有没有任何可能的方法可以使用EF或我在玩火来做类似的事情?如果所有属性都相同,那么必须创建一个名为
CustomerAddress
VendorAddress
的类,这似乎太疯狂了。这几乎就像我需要指定一个EF不允许的双外键


附加说明:我想我将尝试在SQLManagementStudio中设置所有内容,然后在VisualStudio中添加一个数据库第一个EF项目。我很好奇它将如何创建模型和数据库上下文。

看起来客户/供应商和地址类之间的映射不正确

地址表中应该有不同的ForeignKey列指向不同的父表[Customer/Vendor]

因此,更改后,您的实体将如下所示:

地址:

 public int Id { get; set; }
 public string Name { get; set; }
 public string Address1 { get; set; }
 public string Address2 { get; set; }
 public string City { get; set; }
 public string State { get; set; }
 public string Country { get; set; }
 public string ZipCode { get; set; }
 public int CustomerId { get; set; }
 public int VendorId { get; set; }


 // Navigation properties
 public Customer Customer { get; set; }
 public Vendor Vendor { get; set; }
DBContext

builder.Entity<Customer>()
    .HasMany(c => c.Addresses)
    .WithOne(a => a.Customer)
    .HasForeignKey(a => a.CustomerId);

builder.Entity<Vendor>()
    .HasMany(v => v.Addresses)
    .WithOne(a => a.Vendor)
    .HasForeignKey(a => a.VendorId);
builder.Entity()
.HasMany(c=>c.address)
.WithOne(a=>a.Customer)
.HasForeignKey(a=>a.CustomerId);
builder.Entity()
.HasMany(v=>v.address)
.WithOne(a=>a.供应商)
.HasForeignKey(a=>a.VendorId);

希望能有所帮助。

我想你想要的是一种复杂的输入法EF@Chef_Code复杂类型不是我想要的。在一对多关系中,一个客户可以有多个地址,而一个供应商可以有多个地址,这是行不通的。我忘记了这个选项。它没有我想要的那么干净。我真的很想用KeyId。另外,我假设在您的解决方案中CustomerId和VendorId都应该是可空的?是的,CustomerId和VendorId都是可空的字段。如果没有两个单独的外键,EF将无法映射正确的客户:地址或供应商:地址。让我们假设一个场景,其中CustomerId为1和2的Customer表中有两条记录,VendorId为1和2的Vendor表中有两条记录。现在,如果您想将所有这些记录映射到地址表,那么总共将有4条记录具有重复的“KeyId”,这将导致EF在决定哪个KeyId属于哪个客户/供应商时出现问题,希望能够消除您的疑问。谢谢你。。。通常,地址表中会有一个名为Type的附加字段,它是客户或供应商,因此当我选择一个select I would do地址时。其中(a=>a.Type==“Customer”&&a.KeyId==1)。。但我假设没有可能的方法告诉实体框架额外使用类型作为外键的一部分。像这样的人的东西让我想回到使用微型ORM,比如dapper和database first方法。。。似乎有更多的灵活性。嗯。。在这种情况下,您可以从EntityFramework设计SQL复合键,该键可用于多个列。公共类地址{[Key,Column(Order=0)]公共int-KeyId{get;set;}//Customer/Vendor-Id[Key,Column(Order=1)]公共字符串主表{get;set;}//Customer/Vendor}