如何修复一对多关系,其中一方有两个相同类型的实体

How do I fix one to many relationship where one side has two entities of the same type

我正在做一个项目。刚开始的时候,我有一个订单模型和地址模型。但是,现在我想将订单模型更改为具有 AddressTo 和 AddressFrom 而不仅仅是地址。

地址模型:

public class Address
{
    public int AddressId { get; set; }
    public int ZipCodeId { get; set; }
    public string Street { get; set; }
    public int Nr { get; set; }
    public virtual ICollection<Order> Order { get; set; }
    public virtual ZipCode ZipCode { get; set; }
} 

订购型号:

public class Order
{   
    public int OrderId { get; set; }
    public int CustomerId { get; set; }
    public virtual Address Address { get; set; }
    public DateTime OrderDate { get; set; } 
    public DateTime SentDate { get; set; } 
    public string Title { get; set; }
    public string Type { get; set; }
    public string Content { get; set; }
    public int ExpectedHours { get; set; }
    public int AmountWorkers { get; set; }
    public virtual Customer Customer{ get; set; }
}

我想要的订单:

public class Order
{   
    public int OrderId { get; set; }
    public int CustomerId { get; set; }
    public virtual Address AddressTo { get; set; }
    public virtual Address AddressFrom { get; set; }
    public DateTime OrderDate { get; set; } 
    public DateTime SentDate { get; set; } 
    public string Title { get; set; }
    public string Type { get; set; }
    public string Content { get; set; }
    public int ExpectedHours { get; set; }
    public int AmountWorkers { get; set; }
    public virtual Customer Customer{ get; set; }
}

我意识到没有 FluentApi 解决这个问题是不可能的。但是,我发现很难克服这个问题。

我想要的是我的订单 table 在数据库中显示 ID 列 AddressToIdAddressFromId,而不是仅仅 AddressId(这就是正确的方式现在)。

非常感谢社区对此的帮助。

你可以这样配置:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
  // ...
  modelBuilder.Entity<Order>().HasOne(x => x.AddressTo).WithMany(); //Add HasForeignKey, IsRequired... if you want
  modelBuilder.Entity<Order>().HasOne(x => x.AddressFrom).WithMany(); //Add HasForeignKey, IsRequired... if you want 
  //...
}

但是你甚至需要你的 Address 是一个有 id 的实体吗?如果不是,您可以从中删除 AddressIdOrder 属性,并使其成为 Order 实体的 owned entity

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
  // ...
  modelBuilder.Entity<Order>().OwnsOne(x => x.AddressTo);
  modelBuilder.Entity<Order>().OwnsOne(x => x.AddressFrom);
  //...
}

更新:移除订单映射

您可以从 Address class 中删除 Order 属性 并在映射中使用不带参数的 .WithMany()

首先从您的 Address 模型 class 中删除 public virtual ICollection<Order> Order { get; set; } 导航 属性,如下所示:

public class Address
{
    public int AddressId { get; set; }
    public int ZipCodeId { get; set; }
    public string Street { get; set; }
    public int Nr { get; set; }

    public virtual ZipCode ZipCode { get; set; }
}

然后将 AddressFromIdAddressToId 属性添加到您的 Order 模型 class 中,如下所示:

public class Order
{
    public int OrderId { get; set; }
    ..............
    public int AddressFromId { get; set; }
    public virtual Address AddressFrom { get; set; }

    public int AddressToId { get; set; }
    public virtual Address AddressTo { get; set; }

    ................
}

那么你的Order配置如下:

public class OrderConfiguration : IEntityTypeConfiguration<Order>
{
    public void Configure(EntityTypeBuilder<Order> builder)
    {
        builder.HasOne(o => o.AddressFrom).WithMany().HasForeignKey(o => o.AddressFromId)
            .OnDelete(DeleteBehavior.Restrict);

        builder.HasOne(o => o.AddressTo).WithMany().HasForeignKey(o => o.AddressToId)
            .OnDelete(DeleteBehavior.Restrict);
    }
}

然后在DbContextOnModelCreating如下:

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

     modelBuilder.ApplyConfiguration(new OrderConfiguration());  
}