我在实体框架代码优先中为软删除提供了一个可行的解决方案,这可能会有所帮助。
关键是您为每个希望能够软删除的模型添加一个鉴别器。在代码中首先是这样完成的:
modelBuilder.Entity<Foo>().Map(m => m.Requires("IsDeleted").HasValue(false));
这使得它对上下文不可见,因此您必须使用 sql 进行删除。
如果这相当于 Database First 中的“条件映射”,那么修改 sql 的一种方法是覆盖 SaveChanges 并从那里运行 sql:
public override int SaveChanges()
{
foreach (var entry in ChangeTracker.Entries()
.Where(p => p.State == EntityState.Deleted
&& p.Entity is ModelBase))//I do have a base class for entities with a single
//"ID" property - all my entities derive from this,
//but you could use ISoftDelete here
SoftDelete(entry);
return base.SaveChanges();
}
private void SoftDelete(DbEntityEntry entry)
{
var e = entry.Entity as ModelBase;
string tableName = GetTableName(e.GetType());
Database.ExecuteSqlCommand(
String.Format("UPDATE {0} SET IsDeleted = 1 WHERE ID = @id", tableName)
, new SqlParameter("id", e.ID));
//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;
}
用于获取表名的方法解释here
这就是我过去的做法。可能与您在 EF5 中的数据库优先方法无关,但我现在已转向在存储过程中执行此操作。EF6 Code FirstCreateStoredProcedure
在迁移文件中生成调用。我将这些替换为this.CreateDeleteProcedure("dbo.Foo_Delete", "[dbo].[Foos]");
- 这是对我自己的扩展方法的调用:
public static class MigrationExtensions
{
internal static string DeleteSqlFormat
{
//I also hard delete anything deleted more than a day ago in the same table
get { return "DELETE FROM {0} WHERE IsDeleted = 1 AND DATEADD(DAY, 1, DeletedAt) < GETUTCDATE(); UPDATE {0} SET IsDeleted = 1, DeletedAt = GETUTCDATE() WHERE ID = @ID;"; }
}
internal static void CreateDeleteProcedure(this DbMigration migration, string procName, string tableName)
{
migration.CreateStoredProcedure(
procName,
p => new
{
ID = p.Int(),
},
body:
string.Format(MigrationExtensions.DeleteSqlFormat, tableName)
);
}
}