插入具有数据库中存在的许多相关实体的实体 [英] Insert entity with many to many related entities which exist in the database
问题描述
我有两个具有多对多关系的对象:
I have two objects with many to many relationship:
public class Order
{
public int OrderID { get; set; }
public string OrderName { get; set; }
public virtual Collection<Product> Products { get; set; } = new Collection<Product>();
}
public class Product
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public int OrderID { get; set; }
public virtual Collection<Order> Orders { get; set; } = new Collection<Order>();
}
// Mapping table
public class OrderProduct
{
public int OrderId { get; set; }
public Order Order { get; set; }
public int ProductId { get; set; }
public Product Product { get; set; }
}
我想添加一个包含现有产品集合的新订单(我的API会将ProductID数组的输入)输入数据库,因此我的行为如下:
I would like to add a new Order with a collection of existing Products (my API would have an input of ProductID array) into the database, so I perform like:
private void AddOrder(int[] productIDs)
{
Order newOrder = new Order();
newOrder.Name = "New Order";
// Here I have no clue which would be the correct way...Should I do
// Approach A(fetch each related entity - Product from database then add them into the collection of my new base entity - Order):
productIDs.ToList().Foreach(p =>
{
newOrder.Products.Add(_dbContext.Product.FindById(p))
}
);
_dbContext.Orders.Add(newOrder);
var newOrderID = _dbContext.SaveChanges();
// then fetch each product from database and add my new Order to its collection and save
productIDs.ToList().Foreach(p =>
{
var existedProductFromDb = _dbContext.Product.FindById(p);
existedProductFromDb.Orders.Add(newOrder);
_dbContext.SaveChanges();
}
);
}
在实体框架核心中,我真的需要订单和产品之间的映射表吗?否则,解决上述情况的正确方法是什么?
Do I really need the Mapping table between Order and Product in Entity Framework Core? Otherwise, what would be the correct way to deal with above scenario?
推荐答案
您的实体并不代表多对多使用联接表的关系。实际上,您根本不使用联接表。
Your entities do not represent a many-to-many relationship using a joined table. In fact, you don't use the joining table at all.
因此,请先修复您的实体:
So, start by fixing your entities:
public class Order
{
public int OrderID {get;set;}
public string OrderName {get;set;}
public ICollection<OrderProduct> Products { get; set; } = new HashSet<OrderProduct>();
}
public class Product
{
public int ProductID {get;set;}
public string ProductName {get;set;}
public ICollection<OrderProduct> Orders { get; set; } = new HashSet<OrderProduct>();
}
新的订单
其中包含许多 Product
s就像是一个新的 OrderProduct
s的集合一样简单:
A new Order
which contains many Product
s is as simple as a collection of new OrderProduct
s:
private void AddOrder(int[] productIDs)
{
Order newOrder = new Order();
newOrder.Name = "New Order";
foreach (int productId in productIDs)
{
newOrder.Products.Add(new OrderProduct { ProductId = productId });
}
_dbContext.Orders.Add(newOrder);
_dbContext.SaveChanges();
}
此外,请注意 SaveChanges
返回数据库中受影响的行数,而不是插入项的ID。
Also, do notice that SaveChanges
returns the number of affected rows in the database, not the Id of an inserted item.
这篇关于插入具有数据库中存在的许多相关实体的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!