问题
I want to create a generic way to apply soft deletion in my entity framework application. I have an interface model which defines a deleted field:
public interface IModel
{
bool Deleted { get; set; }
Nullable<DateTime> Created { get; set; }
Nullable<DateTime> Modified { get; set; }
}
Almost every model in my application implements this IModel. In order for the DbContext to leave out all records where deleted is set to true, I want to apply a mapping. I've used this nice article as a basis for my implementation.
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<IModel>()
.Map(m => m.Requires("Deleted").HasValue(false))
.Ignore(m => m.Deleted);
}
However, mapping on interfaces is (apparently) not supported by entity framework. This leaves me struggling trying to get this to work in such a way that I do not have to apply this mapping to each model individually.
Is there an other fluent API way for me to apply a mapping on all models which implement IModel without a lot of superfluous code?
来源:https://stackoverflow.com/questions/27857870/entity-framework-workaround-for-mapping-on-interface