Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

336
Vistas
Entity Framework 6 Create() vs new

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!

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

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

over 4 years ago · Santiago Trujillo Denunciar

0

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.

over 4 years ago · Santiago Trujillo Denunciar

0

MyEntity me = new MyEntity();

is equal to

MyEntity me = entities.myentities.Create();

Both of the above create a new instance of MyEntity but neither attach it to the DbSet represented by myentities.

The line

entities.myentities.Add(me)

attaches the instance to the DbSet, though you could use Attach(me) as well.

The "me" is required in the second example as you would be creating an instance of object without a reference to hold the object.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda