开发者

Entity Framework 4 (CTP 5) unwisely queries in comparison with LINQ-to-SQL

I have an issue about Entity Framework 4 CTP 5, which I realize LINQ to SQL handle it better than this, but I insist on using EF 4 because of its Code-First feature.

So here is my problem:

Imagine products with its Tags (or whatever with one to many relation). And tblTags will be changing by deleting some and inserting some tags at a time by user (and user might change them completely different). So in my opinion we can simply delete all old tags and insert new ones (simplest thing to do), although it is a simple idea,开发者_如何学运维 LINQ to SQL manage it perfectly amazing unlike EF 4. The codes for comparison with LINQ are:

LINQ to SQL:

Dim oldTags = ctx.Tags.Where(Function(m) m.ProductID = pID)
Dim newTags = 'from user input, like from the model which was posted, like in MVC'

ctx.Tags.DeleteAllOnSubmit(oldTags)
ctx.Tags.InsertAllOnSubmit(newTags)

ctx.SubmitChanges()

EF 4 CTP5:

Dim oldTags = ctx.Tags.Where(Function(m) m.ProductID = pID)
Dim newTags = 'from user input, like from the model which was posted, like in MVC'
For Each item In oldTags
    ctx.Tags.Remove(item)
Next
For Each item In newTags
    ctx.Product.Add(item)
Next
ctx.SaveChanges()

The point that I'm trying to make is:

  • In LINQ to SQL, it will first try to update old ones (instead of deleting them from DB). Then insert extra tags.
  • But in Entity Framework, it create and send one query per each "remove" and one query per each "add".

EF doesn't calculate the updates, but does as it is told to do(no more). it will send lots of queries through SQL so it will rise my auto number column for no reason.

I know that i can do some logic for the second problem, which then I prefer LINQ to SQL in that way.

But I cant do anything about queries pass to SQL. It sound it is not optimized.

I want Code-First combine with these features that LINQ to SQL currently have(calculating stuff before commit). Any suggestion? Have I done anything wrong in EF code? Or is it gonna be like LINQ to SQL in commit changes in RTM version?

UPDATE

If you don't agree with this behavior please say so and leave a comment.

And If you need more information, please tell me to provide some.

I update this post cause I'm still waiting for an answer. thanks for your time.


You should use batch operation for this:

SaveChanges(SaveChangesOptions.Batch);

Read more at msdn.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜