c# - 如何修复一侧具有两个相同类型实体的一对多关系
问题描述
我正在做一个项目。当我第一次开始时,我有一个订单模型和地址模型。但是,现在我想将 Order 模型更改为具有 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());
}
推荐阅读
- php - PHP 通过 FORM 检查提交的 URL 中的 HTTP 和 HTTPS
- reactjs - React dropzone 组件未显示来自 base64 url 的缩略图
- python - +?标志仍然以贪婪的方式工作
- java - 如何远程调试主项目的 lib 目录中的支持 jars(项目)
- loops - 如何使用 with_lines 在 ansible-playbook 中读取 CSV 文件数据?
- proxy - VPN服务器如何将所有通过的HTTPS请求重定向到固定接口
- php - PHP脚本不断“重新启动”创建自己的新实例
- c# - C# XML 字符串插值
- mql4 - 如何检查哪个 1 是最旧的对象或价格?
- python - numpy 视图如何知道它引用的值在原始 numpy 数组中的位置?