/// <summary>
/// http://stackoverflow.com/questions/19246067/ef-5-conditional-mapping/19248216#19248216
/// http://stackoverflow.com/questions/12698793/soft-delete-entity-framework-code-first/18985828#18985828
/// </summary>
public class Context : DbContext
{ public DbSet<Child> Child { get; set; } public DbSet<Parent> Parent { get; set; }
public Context()
{ Database.SetInitializer<Context>( new MigrateDatabaseToLatestVersion<Context, Configuration>() );
}
protected override void OnModelCreating( DbModelBuilder modelBuilder )
{ modelBuilder.Entity<Child>()
.Map( m => m.Requires( "IsDeleted" ).HasValue( false ) )
.Ignore( m => m.IsDeleted );
modelBuilder.Entity<Parent>()
.Map( m => m.Requires( "IsDeleted" ).HasValue( false ) )
.Ignore( m => m.IsDeleted );
modelBuilder.Conventions.Remove<System.Data.Entity.ModelConfiguration.Conventions.PluralizingTableNameConvention>();
}
public override int SaveChanges()
{ foreach ( var entry in ChangeTracker.Entries()
.Where( p => p.State == EntityState.Deleted ) )
SoftDelete( entry );
return base.SaveChanges();
}
private void SoftDelete( DbEntityEntry entry )
{ Type entryEntityType = entry.Entity.GetType();
string tableName = GetTableName( entryEntityType );
string primaryKeyName = GetPrimaryKeyName( entryEntityType );
string deletequery =
string.Format(
"UPDATE {0} SET IsDeleted = 1 WHERE {1} = @id", tableName, primaryKeyName );
Database.ExecuteSqlCommand(
deletequery,
new SqlParameter( "@id", entry.OriginalValues[primaryKeyName] ) );
//Marking it Unchanged prevents the hard delete
//entry.State = EntityState.Unchanged;
//So does setting it to Detached:
//And that is what EF does when it deletes an item
//http://msdn.microsoft.com/en-us/data/jj592676.aspx
entry.State = EntityState.Detached;
}
private static Dictionary<Type, EntitySetBase> _mappingCache =
new Dictionary<Type, EntitySetBase>();
private EntitySetBase GetEntitySet( Type type )
{ if ( !_mappingCache.ContainsKey( type ) )
{ ObjectContext octx = ( (IObjectContextAdapter)this ).ObjectContext;
string typeName = ObjectContext.GetObjectType( type ).Name;
var es = octx.MetadataWorkspace
.GetItemCollection( DataSpace.SSpace )
.GetItems<EntityContainer>()
.SelectMany( c => c.BaseEntitySets
.Where( e => e.Name == typeName ) )
.FirstOrDefault();
if ( es == null )
throw new ArgumentException( "Entity type not found in GetTableName", typeName );
_mappingCache.Add( type, es );
}
return _mappingCache[type];
}
private string GetTableName( Type type )
{ EntitySetBase es = GetEntitySet( type );
return string.Format( "[{0}].[{1}]", es.MetadataProperties["Schema"].Value,
es.MetadataProperties["Table"].Value );
}
private string GetPrimaryKeyName( Type type )
{ EntitySetBase es = GetEntitySet( type );
return es.ElementType.KeyMembers[0].Name;
}
}