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

后端 未结 1 1748
你的背包
你的背包 2020-12-14 03:51

We can remove single conventions this way:

modelBuilder.Conventions.Remove();
modelBuilder.Conventions.Remove

        
相关标签:
1条回答
  • 2020-12-14 04:34

    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);
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题