对使用实体框架和建立实体之间的关系有疑问。我想要实现的是在集合中拥有来自单独实体的重复项。
List<MenuItem> menuItems = new List<MenuItem>();
MenuItem m1 = menuRepository.GetItemByCode("001");
MenuItem m2 = menuRepository.GetItemByCode("001"); //same item but want to store it additionally as m2.MenuItemExtras will be different from m1.MenuItemExtras
menuItems.Add(m1);
menuItems.Add(m2);
Order order = new Order();
order.MenuItems = menuItems;
orderRepository.Save(order);
这工作正常,但我只将一条记录插入到表中,即
OrderMenuItem
OrderPK = 1, MenuItemPK = 1 //Id of menuItem with code "001"
我可能想要的是这样的:OrderMenuItem 表包含带有它自己的主键的第三列,而不是它是 OrderPK 和 MenuItemPK 的组合。IE
OrderMenuItem
OrderMenuItemId = 1, OrderPK = 1, MenuItemPK = 1
OrderMenuItemId = 2, OrderPK = 1, MenuItemPK = 1
不确定如何实际实现这一目标,以及我是否需要重新定义我的班级关系。感谢您的任何意见。
下面是 DbContext 的类和片段:
public class Order
{
public Guid OrderId { get; set; }
public virtual ICollection<MenuItem> MenuItems { get; set; }
public Order()
{
MenuItems = new Collection<MenuItem>();
}
}
public class MenuItem
{
public Guid MenuItemId { get; set; }
public string Code { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public int SortOrder { get; set; }
public virtual ICollection<MenuItemExtras> MenuItemExtras { get; set; }
}
public class MenuItemCategory
{
public Guid MenuItemCategoryId { get; set; }
public string Name { get; set; }
public string Code { get; set; }
public string HotKey { get; set; }
public int SortOrder { get; set; }
public virtual ICollection<MenuItem> MenuItems { get; set; }
}
public class MenuItemExtras
{
public Guid MenuItemExtrasId { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
在我的 OnModelCreating ovveride 上的 DbContext 中,我有以下内容:
modelBuilder.Entity<MenuItemCategory>().Property(m => m.MenuItemCategoryId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
modelBuilder.Entity<MenuItemCategory>().HasMany(m => m.MenuItems).WithMany();
modelBuilder.Entity<MenuItem>().Property(m => m.MenuItemId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
modelBuilder.Entity<MenuItem>().HasMany(m => m.MenuItemExtras).WithMany();
modelBuilder.Entity<MenuItemExtras>().Property(m => m.MenuItemExtrasId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
modelBuilder.Entity<Order>().Property(o => o.OrderId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
modelBuilder.Entity<Order>().HasMany(m => m.MenuItems).WithMany();
这会生成以下表格:
MenuItem
MenuItemCategory
MenuItemCategoryMenuItem
MenuItemExtras
MenuItemMenuItemExtras
Order
OrderMenuItems