In Entity Framework, create vs. new Object>

.net entity-framework vb.net

Question

I have an entity object initializer/updater. Is using this dangerous?

Dim myObject As SpecialThing = New SpecialThing()

Once all the values have been set, should I use the updater that has already been developed, or should I use:

Dim myObject As SpecialThing = SpecialThing.Create()

The updater has already set the values and handled any issues for the 30 parameters. I'm only trying to reuse that code.

1
12
12/7/2012 5:56:04 AM

Accepted Answer

The item won't be connected to the context if you construct it yourself. For modifications to be updated in the database, you must attach the item.

Though it won't be tied to the context if you create new entity using the Create method, this will still be saved in the database using the SaveChanges method. http://msdn.microsoft.com/en-us/library/gg696136(v=vs.113).aspx

3
1/20/2015 8:06:00 AM

Popular Answer

I'm not sure precisely what you mean bymyDB.CreateSpecialThing(.....) . I see it in three different ways:

  • objectContext.CreateObject<SpecialThing>()

  • dbContext.SpecialThings.Create() (EF >= 4.1)

  • SpecialThing.Create(.....) a formulaic way ofEntityObject entities developed from)

Simply an automatically created helper, the third method only accepts arguments (for the relevant fields), sets properties, and returns the object. It's same to making the item usingnew and then setting properties.

If you are using lazy loading or switching tracking proxies while dealing with POCOs, the first two techniques are applicable. Instead of explicitly creating the entity, these methods will generate a dynamic proxy of the entity (which is a dynamic class created from your entity class). No matter whether you create the object using one of these ways or another, you must manually link it to the context since none of these methods do so.new .

An example of utilizingCreateObject<T> /Create may be significant, assumingUser entity that hasvirtualRoles collection:

using (var ctx = new MyDbContext())
{
    var user = ctx.Users.Create();
    user.Id = 1;
    ctx.Users.Attach(user);

    var roles = user.Roles;
}

Using virtual permits the sluggish loading ofRoles collection and the aforementioned code would load all of User 1's roles (or an empty collection if the user has no roles). Usingnew contrary to that

using (var ctx = new MyDbContext())
{
    var user = new User { Id = 1 };
    ctx.Users.Attach(user);

    var roles = user.Roles;
}

...doesn't permit lazy loading of the collection due touser , a dynamic proxy object, is not.roles as wouldnull regardless on whether the user has roles or not.

Therefore, I would suggest that creating an entity using is not dangerous.new . Just bear in mind that for an entity generated with those tools, you lack the capabilities of slow loading and change tracking proxies.new .



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