添加 child object 到表格 当 parent object 保存到上下文中时
Add child object to tables When parent object saved into context
我已将我的 MVC Web 应用程序的 EntityFramework objectContext 升级到 DBContext 最新版本 v6.1.3。这里使用了 DataBase First 方法
有使用 EDMX 将订单流程添加到数据库的场景。当仅将 Parent table object 保存到上下文中时,以下代码行为将在每个 child table 中添加 objects。这在 ObjectContext 中完美运行。[每个 table 都有新条目 [Order,OrderDetail,license]
但是升级到DBContext后,下面的代码只添加了进入Parent table[Order]。 Child tables 有空记录。在这里,我有超过 10 child table 用于订单处理。仅举几个例子。请提出解决问题的方法。
Table
Order -parent table
OrderDetail -child table of Order
License- child table of Order
代码
using (DBEntities contextentity = new DBEntities ())
{
using (TransactionScope transaction = new TransactionScope())
{
//Parent table
Orders order = new Orders();
order.customerid = 1232;
order.OrderDate = DateTime.Now;
//Child table
OrderDetails orderDetails = new OrderDetails();
orderDetails.Orders = order; //linked parend table
orderDetails.ProductID = 1233;
orderDetails.Quantity = 3;
orderDetails.UnitPrice = product.UnitPrice;
//child table
License license = new License();
license.ProductID = 1233;
license.CustomerId= 1232;
license.LastModifiedDate = DateTime.Now;
license.Orders = order; // linked the parent
//Add the parent table in to context
contextentity.Orders.Add(order);
contextentity.SaveChanges();
transaction.Complete();
}
}
我认为您必须将每个实体添加到其 table。尝试在保存更改之前添加这些行
contextentity.OrderDetails.Add( orderDetails );
contextentity.Lisences.Add( license );
当您使用 ObjectContext
时,您的实体可能不是 POCO,而是源自 EntityObject
,后者自动提供 Orders
及其相关数据之间的跟踪功能(License
和 OrderDetails
) 所以你不必明确地添加 orderDetails
和 license
到上下文。
但是,当您切换到 DbContext
时,EF 不再能够自动检测 license
和 orderDetails
,因此您必须明确添加它们:
contextentity.OrderDetails.Add(orderDetails);
contextentity.Licenses.Add(license);
或者,如果您直接在根对象中公开关系(您应该这样做,因为 orderDetails
- 作为一个值对象 - 不应直接添加到上下文中)EF 将 能够检测到依赖关系,您不必显式添加它们:
public class Orders
{
// assuming each order has many lines
public virtual ICollection<OrderDetails> OrderLines { get; set; }
// assuming each order has many licenses
public virtual ICollection<License> Licenses { get; set; }
// rest of your order data
}
以及连接:
order.OrderLines.Add(orderDetails);
order.Licenses.Add(license);
现在(保存后),子对象的 Orders
导航 属性 也将正确指向父实体,因此您无需手动设置它们。
我已将我的 MVC Web 应用程序的 EntityFramework objectContext 升级到 DBContext 最新版本 v6.1.3。这里使用了 DataBase First 方法
有使用 EDMX 将订单流程添加到数据库的场景。当仅将 Parent table object 保存到上下文中时,以下代码行为将在每个 child table 中添加 objects。这在 ObjectContext 中完美运行。[每个 table 都有新条目 [Order,OrderDetail,license]
但是升级到DBContext后,下面的代码只添加了进入Parent table[Order]。 Child tables 有空记录。在这里,我有超过 10 child table 用于订单处理。仅举几个例子。请提出解决问题的方法。
Table
Order -parent table
OrderDetail -child table of Order
License- child table of Order
代码
using (DBEntities contextentity = new DBEntities ())
{
using (TransactionScope transaction = new TransactionScope())
{
//Parent table
Orders order = new Orders();
order.customerid = 1232;
order.OrderDate = DateTime.Now;
//Child table
OrderDetails orderDetails = new OrderDetails();
orderDetails.Orders = order; //linked parend table
orderDetails.ProductID = 1233;
orderDetails.Quantity = 3;
orderDetails.UnitPrice = product.UnitPrice;
//child table
License license = new License();
license.ProductID = 1233;
license.CustomerId= 1232;
license.LastModifiedDate = DateTime.Now;
license.Orders = order; // linked the parent
//Add the parent table in to context
contextentity.Orders.Add(order);
contextentity.SaveChanges();
transaction.Complete();
}
}
我认为您必须将每个实体添加到其 table。尝试在保存更改之前添加这些行
contextentity.OrderDetails.Add( orderDetails );
contextentity.Lisences.Add( license );
当您使用 ObjectContext
时,您的实体可能不是 POCO,而是源自 EntityObject
,后者自动提供 Orders
及其相关数据之间的跟踪功能(License
和 OrderDetails
) 所以你不必明确地添加 orderDetails
和 license
到上下文。
但是,当您切换到 DbContext
时,EF 不再能够自动检测 license
和 orderDetails
,因此您必须明确添加它们:
contextentity.OrderDetails.Add(orderDetails);
contextentity.Licenses.Add(license);
或者,如果您直接在根对象中公开关系(您应该这样做,因为 orderDetails
- 作为一个值对象 - 不应直接添加到上下文中)EF 将 能够检测到依赖关系,您不必显式添加它们:
public class Orders
{
// assuming each order has many lines
public virtual ICollection<OrderDetails> OrderLines { get; set; }
// assuming each order has many licenses
public virtual ICollection<License> Licenses { get; set; }
// rest of your order data
}
以及连接:
order.OrderLines.Add(orderDetails);
order.Licenses.Add(license);
现在(保存后),子对象的 Orders
导航 属性 也将正确指向父实体,因此您无需手动设置它们。