1、一次性添加映射

 1     public class EntityDbContext : DbContext 
 2     {  
 3         public EntityDbContext()
 4             : base("name=test2")
 5         { }
 6 
 7 
 8         /// <summary>
 9         /// 通过反射一次性将表进行映射
10         /// </summary>
11         /// <param name="modelBuilder"></param>
12         protected override void OnModelCreating(DbModelBuilder modelBuilder)
13         {
14            
15             var typesRegister = Assembly.GetExecutingAssembly().GetTypes()
16                 .Where(type => !(string.IsNullOrEmpty(type.Namespace))).Where(type => type.BaseType != null && type.BaseType.IsGenericType && type.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>));
17             foreach (var type in typesRegister)
18             {
19                 dynamic configurationInstance = Activator.CreateInstance(type);
20                 modelBuilder.Configurations.Add(configurationInstance);
21             }
22 
23         }
24     }
View Code

相关文章: