简体   繁体   中英

Updating a record using Entity Framework context without manually setting properties

I am trying to update a record using Entity Framework but want to avoid manually setting each property and then calling save changes. I have an instance of this class bound to a dataform that updates all its properties as a user changes them. The nicest way I can see using this would be to set the object in the context equal to the object bound to my dataform but this does not work. I tried removing the object from the context then adding the dataform bound object but it creates a completely new record (I expected this but decided it was worth a shot to try it out). I assume there is some type of flag with in the context that is used to detect if a record is a new row or not considering the fact that replacing the context object with the databound object (which has the same pk value) does not work.

using (var scope = new TransactionScope())
        {
            try
            {
                using (var context = new CIS_DEVEntities())
                {
                    GangMemberBio bio = context.GangMemberBios.First(P => P.id == this.id);
                    bio = this; //this does not work.
                    context.SaveChanges();
                }
                //If the top code is successfully completed then the transaction will be commited
                //if not this line will be skipped and execution will be given to the catch block
                scope.Complete();
            }
            catch
            {

            }
        }

Edit Idea 1 I was thinking I could create a context object on my wpf window itself and then bind the dataform to my gangmemberbio object retrieved from this context. Then when I call save changes nothing else needs to be done. I heard that having a datacontext in memory is bad though.. Or is it just bad if I use it outside the context of a using statement?

In your edit page, load the object from the database first which will cause your form's fields to all be populated with existing data from the database. When the form is posted then, all the values of your data object model will be set.

DbContext keeps track of each entity's state internally, so it knows whether to add/update/delete any given one. When you Add() an entity to the context, even though it might already have an id, it's default state is Added (might be New , not sure), which causes the existing id to be ignored and a new record inserted into the db.

What you want to do is attach the already existing entity to the DbContext, and then let the DbContext know that it's to be treated as an existing entity so that it's updated, rather than inserted.

You do that by setting the EntityState in the DbContext for the given entity to Modified (by default it's New when adding an entity to the DbContext).

Try something like this:

using (var context = new CIS_DEVEntities())
{
    context.Entry(this).State = EntityState.Modified;
    context.SaveChanges();
}

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