简体   繁体   English

LINQ2SQL与EF4:来自不同上下文的实体检索和使用

[英]LINQ2SQL vs EF4: Entity retrieval and usage from different contexts

I am long time user of LINQ2SQL, but have not used the Entity Framework yet. 我是LINQ2SQL的长期用户,但尚未使用实体框架。

One thing that is not possible in LINQ2SQL is to use tracked entities in different data contexts, or 'link' objects from different data contexts. 在LINQ2SQL中不可能做的一件事是在不同的数据上下文中使用跟踪的实体,或者在不同的数据上下文中使用“链接”对象。

Example: 例:

Foo f = null;

using (var dc = new DB()) 
  f = dc.Foos.Single(x => x.ID = 1);

using (var dc = new DB()) 
{
  var b = new Baz();
  dc.Bazs.InsertOnSubmit(b);
  f.Baz = b;
  dc.SubmitChanges();
}

Note: IIRC, this can work if using disconnected objects (but IMO that is pretty useless). 注意:IIRC,如果使用断开连接的对象,这可以工作(但是IMO几乎没有用)。

Today, I saw an article on EF4 implying that the pattern above can be used with EF4. 今天,我看到了一篇有关EF4文章,暗示上述模式可以与EF4一起使用。

So the question is: Is this in fact possible? 所以问题是:这实际上可能吗?

no you cannot 你不能

you stil lhave to faff about with detaching and attaching to a new data context. 您仍然不愿意分离和附加到新的数据上下文。 Invariably you end up doing this by using the unique key for the object to slect it out of the database and then copy the properties into the new object and save the changes back. 通过使用对象的唯一键将其从数据库中选择出来,然后将属性复制到新对象中,然后将所做的更改保存回去,最终您总是会这样做。

Sad but true. 悲伤但真实。

I used to be a lin2sql junkie, i do prefer EF4 though after a bit of exposure to it. 我曾经是lin2sql迷,但在接触了EF4之后,我还是更喜欢EF4。 I like the various forms of inheritance - they can be leveraged to do some interesting things. 我喜欢继承的各种形式-可以利用它们来做一些有趣的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM