Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between these two statements (Entity Framework)

Does any one know the difference, if any, of the following statements?

_context.AddObject(user);

_context.Users.AddObject(user);

as well as

_context.Attach(user);

_context.Users.Attach(user);

Thanks

EDIT

Sorry some confusion:

I know the difference between AddObject and Attach, what I meant was is there any difference in the way you use AddObject i.e.

_context.AddObject(user);
_context.Users.AddObject(user);
like image 348
Peuge Avatar asked Nov 14 '22 09:11

Peuge


1 Answers

An ObjectContext can have multiple sets with the same schema, so it's normally better to use the explicit set. ie _context.Users.AddObject(user);

From MSDN:

In the .NET Framework version 4, we recommend that you use methods on the ObjectSet object to perform create, read, delete, attach, and update operations. ObjectSet derives from ObjectQuery, so it also works as a query object.

In versions starting with .NET Framework version 4, you can use the following methods defined on ObjectSet instead of the equivalent ones defined on ObjectContext:

 AddObject   
 Attach
 ApplyCurrentValues
 ApplyOriginalValues
 DeleteObject
 Detach
like image 194
GazTheDestroyer Avatar answered Nov 16 '22 03:11

GazTheDestroyer