Entity Framework 6 Create() vs new

c# entity-framework-6

Question

What is the difference between adding an entity in these two ways?

MyEntity me = new MyEntity();
entities.myentities.Add(me);

vs

MyEntity me = entities.myentities.Create();

Do I still need to add "me" in the second example? If so, is there some sort of advantage, one way or the other?

Many thanks!

1
16
7/14/2015 12:00:30 PM

Accepted Answer

MyEntity me = new MyEntity();

will create a new instance of MyEntity

MyEntity me = entities.myentities.Create();

will create a proxy wrapped instance of MyEntity (assuming your context is configured to create proxies)

This proxy overrides some virtual properties of the entity to insert hooks for performing actions automatically when the property is accessed. For example, this mechanism is used to support lazy loading of relationships.

from here

16
7/14/2015 12:20:46 PM

Popular Answer

Yes, you still need to add it. From the documentation of the Create method:

Creates a new instance of an entity for the type of this set. Note that this instance is NOT added or attached to the set.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow