开发者

EF in .NET 3.5 insert child rows in wrong order

开发者 https://www.devze.com 2023-03-04 06:53 出处:网络
I use Entity Framework in my ASP.NET application (.NET 3.5) My model is simple: Order (Id) Product (Id, OrderId)

I use Entity Framework in my ASP.NET application (.NET 3.5) My model is simple:

Order (Id)
Product (Id, OrderId)
Category (Id, Pr开发者_如何转开发oductId, 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 ?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消