Entity framework workaround for mapping on interface

对着背影说爱祢 提交于 2019-12-08 04:13:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!