简体   繁体   中英

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.

One thing that is not possible in LINQ2SQL is to use tracked entities in different data contexts, or 'link' objects from different data contexts.

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).

Today, I saw an article on EF4 implying that the pattern above can be used with 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. I like the various forms of inheritance - they can be leveraged to do some interesting things.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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