public static T InsertOrUpdate <T>(this FawDataContext context, T entity, object key) where T : class, new() { var dbSet = context.Set <T>(); var dbEntity = dbSet.Find(key); if (dbEntity == null) { dbEntity = dbSet.Create(); dbSet.Add(dbEntity); } context.Entry(dbEntity).CurrentValues.SetValues(entity); return(dbEntity); }
public static T GetDetachedCopy <T>(this FawDataContext context, T entity) where T : class { var proxyCreationEnabled = context.Configuration.ProxyCreationEnabled; try { context.Configuration.ProxyCreationEnabled = false; var poco = context.Entry(entity).CurrentValues.ToObject() as T; return(poco); } finally { context.Configuration.ProxyCreationEnabled = proxyCreationEnabled; } }
public static T Clone <T>(this FawDataContext context, T entity, bool?makeProxy = null) where T : class { var proxyCreationEnabled = context.Configuration.ProxyCreationEnabled; try { context.Configuration.ProxyCreationEnabled = makeProxy ?? IsProxy(entity); var clone = context.Set <T>().Create(); var entry = context.Entry(clone); entry.State = EntityState.Added; entry.CurrentValues.SetValues(entity); entry.State = EntityState.Detached; return(clone); } finally { context.Configuration.ProxyCreationEnabled = proxyCreationEnabled; } }