簡體   English   中英

如何解決一對多關系,其中一側有兩個相同類型的實體

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

我正在做一個項目。 剛開始時,我有一個訂單模型和地址模型。 但是,現在,我想將訂單模型更改為具有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 (這就是現在的樣子)。

非常感謝社區對此的幫助。

您可以這樣配置:

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實體的擁有實體:

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

更新:刪除訂單映射

您可以從Address類中刪除Order屬性,而只需在映射中使用不帶參數的.WithMany()即可。

首先刪除public virtual ICollection<Order> Order { get; set; } public virtual ICollection<Order> Order { get; set; } 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());  
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM