行插入订单实体框架 [英] Row insertion order entity framework

查看:132
本文介绍了行插入订单实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个事务在多个表中插入多行。对于这些行,我想按顺序添加这些行。调用SaveChanges后,所有行都将按顺序插入。



在每次插入之后不使用事务并保存更改时都会保持顺序,但是我真的需要所有的事务条目。

解决方案

实体框架中的订单插入/更新和删除取决于实体框架中的许多内容。



例如,如果您在新类别中插入新产品,我们必须在产品之前添加类别。



<这意味着如果你有一个很大的变化,那么我们必须首先满足当地的排序限制,这就是我们所做的。



你在上下文中做的事情可能与这些规则相冲突。例如,如果您这样做:

  ctx.AddToProducts(
new Product {
Name =Bovril ,
Category = new Category {Name =Food}
}
);效果是首先添加产品(到上下文),然后当我们走图时我们也添加了类别。



ie在上下文中插入订单是:

 产品
类别
/ pre>

但是由于参照完整性限制,我们必须重新排序,然后再尝试插入数据库:

 类别
产品

所以这样本地重新订购是一种不可协商的方式。



但是,如果没有这样的本地依赖,您可以理论上保留订购。不幸的是,我们目前没有跟踪什么时候的内容,为了效率,我们不跟踪实体,以保持结构像列表。因此,我们目前无法保留无关插入的顺序。



然而,我们最近正在辩论这个问题,所以我很想看到这是至关重要的你是吗?



希望这有帮助



Alex



项目经理实体框架小组


I'm using a transaction to insert multiple rows in multiple tables. For these rows I would like to add these rows in order. Upon calling SaveChanges all the rows are inserted out of order.

When not using a transaction and saving changes after each insertion does keep order, but I really need a transaction for all entries.

解决方案

The order inserts/updates and deletes are made in the Entity Framework is dependent upon many things in the Entity Framework.

For example if you insert a new Product in a new Category we have to add the Category before the Product.

This means that if you have a large set of changes there are local ordering constraints that we must satisfy first, and indeed this is what we do.

The order that you do things on the context can be in conflict with these rules. For example if you do this:

ctx.AddToProducts(
   new Product{
      Name = "Bovril",
      Category = new Category {Name = "Food"}
   }
);

the effect is that the Product is added (to the context) first and then when we walk the graph we add the Category too.

i.e. the insert order into the context is:

Product
Category

but because of referential integrity constraints we must re-order like this before attempting to insert into the database:

Category
Product

So this kind of local re-ordering is kind of non-negotiable.

However if there are no local dependencies like this, you could in theory preserve ordering. Unfortunately we don't currently track 'when' something was added to the context, and for efficiency reason we don't track entities in order preserving structures like lists. As a result we can't currently preserve the order of unrelated inserts.

However we were debating this just recently, so I am keen to see just how vital this is to you?

Hope this helps

Alex

Program Manager Entity Framework Team

这篇关于行插入订单实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆