我有一张桌子 - 乐器。该表具有InstrumentId的PK和Book,Page,DocumentTypeId等列
我有另一张桌子 - 订单。此表存储有关用户创建的过滤器的信息。 PK是OrderId,它有像OrderName等的列
这个想法是用户创建订单并定义订单的过滤器。应用过滤器时,将返回一组匹配的仪器。然后,用户将查看匹配并为每个匹配添加注释和/或“收藏”标记。
我需要存储每个订单的匹配工具列表以及用户注释等。
由于多个用户订单可能会引用相同的工具,因此我不能简单地向工具添加注释并在一天内调用它。
通常,我会添加第三个名为Matches的表。该表将包含以下列:OrderId,InstrumentId,IsFavorite,Notes。基本上,连接表包含一些额外的字段。
如果它是一个简单的连接问题(没有IsFavorite或Notes),我将执行以下操作(假设已经创建了POCO对象区域):
public OrderConfiguration()
{
ToTable("orders");
HasKey("orderid");
...
HasMany(o => o.Matches).WithMany().Map(x =>
{
x.ToTable("matches");
};
}
public InstrumentConfiguration()
{
ToTable("instruments");
HasKey("instrumentid");
...
}
但是,我是否正确,因为我有两个实体(订单和工具)之间的关系的其他属性(IsFavorite& Notes),我现在必须引入一个新表和一个新实体? IOW,我不能将这些列放入我的Matches表中并以某种方式将它们映射正确吗?我必须添加一个新实体 - MatchedInstrument? MatchedInstrument看起来像:
public class MatchedInstrument
{
public int Id {get; set; }
public int InstrumentId {get; set;}
public bool IsFavorite {get; set;}
public string Notes {get; set;}
}
然后我会添加一个具有Id,IntrumentId等的MatchedInstrument表。然后我必须将Matches表更改为具有OrderId和MatchedInstrumentId?
这是处理此问题的正确方法吗?
答案 0 :(得分:0)
我会创建如下关系。
public class AppContext : DbContext
{
public DbSet<Instrument> Instruments { get; set; }
public DbSet<Order> Orders { get; set; }
}
public class Instrument
{
public int Id { get; set; }
public string Book { get; set; }
public string Page { get; set; }
public virtual ICollection<MatchedInstrument> MatchedInstruments { get; set; }
}
public class Order
{
public int Id { get; set; }
public string OrderName { get; set; }
public virtual ICollection<MatchedInstrument> MatchedInstruments { get; set; }
}
public class MatchedInstrument
{
public int Id { get; set; }
public int InstrumentId { get; set; }
public Instrument Instrument { get; set; }
public int OrderId { get; set; }
public Order Order { get; set; }
public bool IsFavorite { get; set; }
public string Notes { get; set; }
}