/// <summary> /// Extension method used to attach the specified entity as modified, /// with the specified original state. This is a non-generic version. /// </summary> /// <param name="dbSet">The <see cref="System.Data.Entity.DbSet"/> to attach to.</param> /// <param name="current">The current entity.</param> /// <param name="original">The original entity.</param> /// <param name="dbContext">The corresponding <see cref="System.Data.Entity.DbContext"/></param> public static void AttachAsModified(this DbSet dbSet, object current, object original, DbContext dbContext) { if (dbSet == null) { throw Error.ArgumentNull("dbSet"); } if (current == null) { throw Error.ArgumentNull("current"); } if (original == null) { throw Error.ArgumentNull("original"); } if (dbContext == null) { throw Error.ArgumentNull("dbContext"); } var entityEntry = dbContext.Entry(current); if (entityEntry.State == EntityState.Detached) { dbSet.Attach(current); } else { entityEntry.State = EntityState.Modified; } var objectContext = (dbContext as IObjectContextAdapter).ObjectContext; var stateEntry = ObjectContextUtilities.AttachAsModifiedInternal(current, original, objectContext); if (stateEntry.State != EntityState.Modified) { // Ensure that when we leave this method, the entity is in a // Modified state. For example, if current and original are the // same, we still need to force the state transition entityEntry.State = EntityState.Modified; } }
/// <summary> /// Extension method used to attach the specified entity as modified, /// with the specified original state. /// </summary> /// <typeparam name="T">The entity Type</typeparam> /// <param name="dbSet">The <see cref="DbSet"/> to attach to.</param> /// <param name="current">The current entity.</param> /// <param name="original">The original entity.</param> /// <param name="dbContext">The corresponding <see cref="DbContext"/></param> public static void AttachAsModified <T>(this DbSet <T> dbSet, T current, T original, DbContext dbContext) where T : class { if (dbSet == null) { throw new ArgumentNullException("dbSet"); } if (current == null) { throw new ArgumentNullException("current"); } if (original == null) { throw new ArgumentNullException("original"); } if (dbContext == null) { throw new ArgumentNullException("dbContext"); } DbEntityEntry <T> entityEntry = dbContext.Entry(current); if (entityEntry.State == EntityState.Detached) { dbSet.Attach(current); } else { entityEntry.State = EntityState.Modified; } ObjectContext objectContext = (dbContext as IObjectContextAdapter).ObjectContext; ObjectStateEntry stateEntry = ObjectContextUtilities.AttachAsModifiedInternal <T>(current, original, objectContext); if (stateEntry.State != EntityState.Modified) { // Ensure that when we leave this method, the entity is in a // Modified state. For example, if current and original are the // same, we still need to force the state transition entityEntry.State = EntityState.Modified; } }
/// <summary> /// Extension method used to attach the specified entity as modified, /// with the specified original state. /// </summary> /// <typeparam name="T">The entity Type</typeparam> /// <param name="objectSet">The ObjectSet to attach to</param> /// <param name="current">The current entity state</param> /// <param name="original">The original entity state</param> public static void AttachAsModified <T>(this ObjectSet <T> objectSet, T current, T original) where T : class { if (objectSet == null) { throw new ArgumentNullException("objectSet"); } if (current == null) { throw new ArgumentNullException("current"); } if (original == null) { throw new ArgumentNullException("original"); } // Attach the entity if it is not already attached, or if it is already // attached, transition to Modified EntityState currState = ObjectContextUtilities.GetEntityState(objectSet.Context, current); if (currState == EntityState.Detached) { objectSet.Attach(current); } else { objectSet.Context.ObjectStateManager.ChangeObjectState(current, EntityState.Modified); } ObjectStateEntry stateEntry = ObjectContextUtilities.AttachAsModifiedInternal(current, original, objectSet.Context); if (stateEntry.State != EntityState.Modified) { // Ensure that when we leave this method, the entity is in a // Modified state. For example, if current and original are the // same, we still need to force the state transition objectSet.Context.ObjectStateManager.ChangeObjectState(current, EntityState.Modified); } }