实体框架4(CTP 5)与LINQ-to SQL相比不寻常的查询 [英] Entity Framework 4 (CTP 5) unwisely queries in comparison with LINQ-to-SQL

查看:164
本文介绍了实体框架4(CTP 5)与LINQ-to SQL相比不寻常的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于Entity Framework 4 CTP 5的问题,我认识到LINQ to SQL比这更好,但我坚持使用EF 4,因为它的Code-First功能。



所以这里是我的问题:



想象一下,其标签无论与一对多关系)。而且,tblTags 将通过删除一些并且用户一次插入一些标签来进行更改(并且用户可能会将它们完全不同)。所以在我看来,我们可以简单地删除所有旧的标签并插入新的标签(最简单的事情),尽管这是一个简单的想法,LINQ to SQL管理它与EF 4非常惊人。与LINQ进行比较的代码是: p>

LINQ to SQL:

  Dim oldTags = ctx.Tags.Where函数(m)m.ProductID = pID)
从用户输入中调用newTags =',就像从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 ='从用户输入,像从发布的模型,像MVC'
对于每个项目在oldTags
ctx.Tags.Remove(item)
Next
对于每个项目在newTags
ctx.Product.Add(item)
下一个
ctx.SaveChanges()

我尝试的一点在




  • 在LINQ to SQL中,它将首先尝试更新
    旧的(而不是删除它们) DB)。然后插入额外的标签。

  • 但是在实体框架中,它会根据每个删除创建一个查询,并为每个添加一个
    查询 EM>。



EF不计算更新,但按照被告知要做的(不再)。它将通过SQL发送大量查询,所以它将无缘无故地升高我的自动号码列。

我知道我可以为第二个问题做一些逻辑,然后我更喜欢LINQ to SQL 。

但是我无法做任何关于查询传递给SQL的事情。它听起来没有优化。



我希望代码优先结合LINQ to SQL目前拥有的这些功能(在提交之前计算内容)。任何建议? EF代码有没有错误?或者在RTM版本的变更中会像LINQ to SQL一样吗?



更新

如果你不同意这个行为,请说出来并发表评论。

如果您需要更多信息,请告诉我提供一些。



我更新这篇文章,因为我还在等待答案。谢谢你的时间。

解决方案

您应该使用批量操作:

 的SaveChanges(SaveChangesOptions.Batch); 

阅读更多 msdn


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.

这篇关于实体框架4(CTP 5)与LINQ-to SQL相比不寻常的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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