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

问题描述 投票:1回答:2

我正在做一个项目。当我第一次开始时,我有一个订单模型和地址模型。但是,现在,我想将Order Model更改为AddressTo和AddressFrom,而不仅仅是Address。

地址型号:

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解决这个问题是不可能的。但是,我发现很难克服这个问题。

我想要的是我的数据库中的Order表,以显示ID列AddressToIdAddressFromId,而不仅仅是AddressId(现在就是这样)。

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

c# entity-framework-core one-to-many ef-fluent-api
2个回答
0
投票

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

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模型类中,如下所示:

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);
    }
}

然后在OnModelCreatingDbContext如下:

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

     modelBuilder.ApplyConfiguration(new OrderConfiguration());  
}

0
投票

您可以像这样配置它:

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属性,并使其成为owned entity实体的Order

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

更新:已删除订单映射

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

© www.soinside.com 2019 - 2024. All rights reserved.