我正在做一个项目。当我第一次开始时,我有一个订单模型和地址模型。但是,现在,我想将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列AddressToId
和AddressFromId
,而不仅仅是AddressId
(现在就是这样)。
非常感谢社区对此的帮助。
首先从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; }
}
然后将AddressFromId
和AddressToId
属性添加到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);
}
}
然后在OnModelCreating
的DbContext
如下:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfiguration(new OrderConfiguration());
}
您可以像这样配置它:
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的实体吗?如果没有,你可以从中删除AddressId
和Order
属性,并使其成为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()
。