I am using the entity framework and I\'m having a problem with \"re-finding\" objects I just created... basically it goes like this:
string theId = \"someId
The extension method bellow is to DbSet<>
public static T TryAttach(this DbSet dbSet, T entity, Expression> predicate) where T : class
{
T found = dbSet.Local.SingleOrDefault(predicate.Compile());
if (found == null) dbSet.Attach(entity);
return found ?? entity;
}
How to use:
contextInstance.MyEntity.TryAttach(entityInstance, e => e.ID == entityInstance.ID);
btw: I love generics!