Generic Any/Attach/Add function for Entity Framework

Posted by Matt Thrower on Stack Overflow See other posts from Stack Overflow or by Matt Thrower
Published on 2013-11-07T09:19:12Z Indexed on 2013/11/07 9:54 UTC
Read the original article Hit count: 278

Filed under:
|
|

Looking through my EF classes, they're littered with code that looks like this:

if (_myContext.[EntityType].Any(d => d.RowId == dc.RowId))  
{
    _myContext.[EntityType].Attach(dc);
    _myContext.Entry(dc).State = EntityState.Modified;
}
else
{
    _myContext.[EntityType].Add(dc);
}

It's the same thing over and over, and is clearly itching to be handled by a generic function.

However, I'm not sure how you'd go about handling the need for it to deal with a variety of unexpected entity types. A good example to get me started would be most appreciated.

© Stack Overflow or respective owner

Related posts about c#

Related posts about iphone