简体   繁体   中英

Entity Framework 6 Updating a record manually

I am using EF 6 with WPF. Since I have to work with lots of DataSets, I have to use AsNoTracking() with the queries. Thus, updating any of the entries is now a responsibility of the ViewModel. I am using repository pattern , and I am thinking of implementing a method in the generic repository like this:

virtual public void Update(T updatedentity)
{
    _ctx.Set<T>().Attach(updatedentity);
    _ctx.Entry(updatedentity).State = EntityState.Modified;
}

Is this a good idea? What are the pros and cons of this approach? And finally, will there be the significant performance hit? A point to note here, most of my entities have at most 15-20 attributes.

Yes, you can/should attach entities that already exist in the database and then setting it's state to Modified will force all T entity properties to be updated.

You can read a good explanation of Attach method here: Entity Framework 4 - AddObject vs Attach Please also read possible impacts of EntityState.Modified here @Gert answer: Entity Framework - Why explicitly set entity state to modified?

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