EF 4.1, Code-First: Is there an easy way to remove ALL conventions?

一个人想着一个人 提交于 2019-11-28 19:38:04

I just create some solution with reflection:

public class Context : DbContext
{
    private static IList<Type> _types = typeof(IConvention).Assembly.GetTypes()
        .Where(t => !t.IsAbstract && t.IsClass && 
                    typeof(IConvention).IsAssignableFrom(t))
        .ToList();

    private static MethodInfo _method = 
        typeof(ConventionsConfiguration).GetMethod("Remove");

    // DbSets ...

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        foreach (var type in _types)
        {
            MethodInfo method = _method.MakeGenericMethod(type);
            method.Invoke(modelBuilder.Conventions, null);
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!