.NET 3.5 中的 EF 以错误的顺序插入子行
我在 ASP.NET 应用程序 (.NET 3.5) 中使用实体框架 我的模型很简单:
Order (Id)
Product (Id, OrderId)
Category (Id, ProductId, NameA, NameB)
我的代码如下所示:
StoreEntity db = new StoreEntity();
Order order = GetOrder(1);
Product product = new Product();
product.Categories.Add(new Category() { NameA = "1" });
product.Categories.Add(new Category() { NameA = "2" });
product.Categories.Add(new Category() { NameA = "3" });
product.Categories.Add(new Category() { NameB = "A" });
product.Categories.Add(new Category() { NameB = "B" });
product.Categories.Add(new Category() { NameB = "C" });
order.Products.Add(product);
db.SaveChanges();
问题是数据库中的类别顺序错误,即:
CategoryId,ProductId,NameA,NameB
1,1,3,NULL
2,1,2,NULL
3,1,1,NULL
4,1,NULL,C
5,1,NULL,B
6,1,NULL,A
当我切换到 .NET 4 时,行顺序是正确的。 3.5 有办法解决这个问题吗?
I use Entity Framework in my ASP.NET application (.NET 3.5)
My model is simple:
Order (Id)
Product (Id, OrderId)
Category (Id, ProductId, NameA, NameB)
My code looks like:
StoreEntity db = new StoreEntity();
Order order = GetOrder(1);
Product product = new Product();
product.Categories.Add(new Category() { NameA = "1" });
product.Categories.Add(new Category() { NameA = "2" });
product.Categories.Add(new Category() { NameA = "3" });
product.Categories.Add(new Category() { NameB = "A" });
product.Categories.Add(new Category() { NameB = "B" });
product.Categories.Add(new Category() { NameB = "C" });
order.Products.Add(product);
db.SaveChanges();
Problem is that in database categories are in wrong order, that is:
CategoryId,ProductId,NameA,NameB
1,1,3,NULL
2,1,2,NULL
3,1,1,NULL
4,1,NULL,C
5,1,NULL,B
6,1,NULL,A
When I switch to .NET 4 row order is correct. Is there a way to fix this in 3.5 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,没有 EF 版本可以确保项目按照应用程序中创建的顺序插入。如果它在 EFv4 中有效,那很可能只是运气,因为我有很多例子,其中 EFv4 中的顺序也不同。
As I know no EF version will ensure that items will be inserted in the same order as created in the application. If it works in EFv4 it will be most probably just luck because I have many examples where order is different in EFv4 as well.