Entity framework 实体框架4.1代码优先-如何在两个表之间创建两种不同的关系

Entity framework 实体框架4.1代码优先-如何在两个表之间创建两种不同的关系,entity-framework,entity-framework-4.1,ef-code-first,fluent-interface,Entity Framework,Entity Framework 4.1,Ef Code First,Fluent Interface,我需要创建一个关系,其中我有一个链接到地址表的用户表。问题是我需要地址表来存储历史地址。用户也可能根本没有地址 public class user { public virtual int ID { get; set; } ... public virtual int? AddressId { get; set; } [ForeignKey("AddressId")] public virtual Address CurrentAddress { get; set; }

我需要创建一个关系,其中我有一个链接到地址表的用户表。问题是我需要地址表来存储历史地址。用户也可能根本没有地址

public class user
{
  public virtual int ID { get; set; }
  ...
  public virtual int? AddressId { get; set; }

  [ForeignKey("AddressId")]
  public virtual Address CurrentAddress { get; set; }

  public virtual ICollection<Address> HistoricAddresses { get; set; }
}

public class Address
{
  public virtual int ID { get; set; }
  ...
}
公共类用户
{
公共虚拟整数ID{get;set;}
...
公共虚拟整数?地址ID{get;set;}
[外键(“地址ID”)]
公共虚拟地址CurrentAddress{get;set;}
公共虚拟ICollection HistoricadAddresss{get;set;}
}
公共课堂演讲
{
公共虚拟整数ID{get;set;}
...
}
我尝试了各种方法来实现此功能,但出现了各种错误,例如在用户和地址之间放置了另一个表:

public class HistoricAddress
{
  public virtual int ID { get; set; }
  public Address HistoricAddress { get; set; }
}

public class user
{
  public virtual int ID { get; set; }
  public virtual Address CurrentAddress { get; set; }
  public virtual ICollection<HistricAddress> HistoricAddresses { get; set; }
  ...
}
公共类历史记录
{
公共虚拟整数ID{get;set;}
公共广播历史地址{get;set;}
}
公共类用户
{
公共虚拟整数ID{get;set;}
公共虚拟地址CurrentAddress{get;set;}
公共虚拟ICollection HistoricadAddresss{get;set;}
...
}
和其他各种方式,但这也会产生错误。必须有一个适当的方法来做到这一点。最后一个错误是:


“System.Data.Entity.Infrastructure.DbUpdateException:更新条目时出错。有关详细信息,请参见内部异常。-->System.Data.UpdateException:更新条目时出错。有关详细信息,请参见内部异常。-->System.InvalidOperationException:引用约束中的依赖属性映射到存储生成的列。列:“ID”。

您可以用3个表映射以下模型

public class user
{
  public virtual int ID { get; set; }
  ...
  public virtual int? AddressId { get; set; }

  public virtual Address CurrentAddress { get; set; }

  public virtual ICollection<Address> HistoricAddresses { get; set; }
}

public class Address
{
  public virtual int ID { get; set; }

  public virtual ICollection<User> Users { get; set; }
  ...
}
创建的表是

  • 使用者
  • 地址
  • 用户地址

您的第一个代码段有什么问题?代码优先约定应该正确映射,而无需进一步配置
用户
地址
之间的两个关系,不是吗?您是说两个表,因为我看不到第三个表在这里的位置。我还遇到了第二个fluent api的问题映射(.HasForeignKey在多对多关系上不受支持)。我在没有.HasForeignKey的情况下尝试了它,但在运行代码时遇到了与我在帖子中提到的错误相同的错误。@JohnCambell第三个表是
UserAddresses
。.HasForeignKey是错误添加的。请查看我更新的答案。现在得到了第三个表位,我想到了实体。@JohnCambell是的,只有2个实体。我想nk关系正在工作,但尝试获取数据时失败。一旦我将地址分配给用户,就会导致“引用约束中的依赖属性映射到存储生成的列”。列:“ID”错误。我想先将地址添加到集合中,然后用CurrentAddressId分配给它,但在保存用户之前,它如何知道事务中的地址。
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<User>()
        .HasOptional(user => user.CurrentAddress).WithMany()
        .HasForeignKey(user => user.AddressId);

    modelBuilder.Entity<User>()
        .HasMany(user => user.HistoricAddresses)
        .WithMany(address => address.Users)
            .Map(m =>
            {
                m.ToTable("UserAddresses");
                m.MapLeftKey("UserId");
                m.MapRightKey("AddressId");
            });
}