OSharp DbContent初始化分析

DBContent初始化 —— 关联Entity查找

一、      关联到具体的Entity

 

二、      通过EntityTypeConfiguration 关联到DbContent

 

三、      在初始化DbContent时,映射相应的Entity对象

 

四、      通过缓存DbContentType集合字典,查找DbContextInitializerBase

 

五、      查找当前DbContent的Entity 

六、      具体DbContent创建(自定义DbContent的实现)

需实现DbContextInitializerBase基类

 

筛选出当前DbContent的Entity

 

初始化配置

 

 

DBContent初始化 —— DBContent生成

一、      主要方法

 

二、      框架初始化入口

 

三、      DatabaseInitializer数据库初始化——配置信息

 

四、      DatabaseInitializer数据库初始化——生成

 

附一段单元测试代码

  1 public class DbContentInitializeTest
  2     {
  3         [Fact()]
  4         public void DbContentInitialize_Test()
  5         {
  6             using (var context = new MyDbContext())
  7             {
  8                 context.AddEntityRegHelper(new CustomerRegstHelper());
  9                 context.AddEntityRegHelper(new CusRegstHelper());
 10                 IDatabaseInitializer<MyDbContext> initializer;
 11                 if (!context.Database.Exists())
 12                 {
 13                     initializer = new CreateDatabaseIfNotExists<MyDbContext>(); ;
 14                 }
 15                 else
 16                 {
 17                     initializer = new MigrateDatabaseToLatestVersion<MyDbContext, AutoMigrationsConfiguration<MyDbContext>>(); ;
 18                 }
 19                 Database.SetInitializer(initializer);
 20 
 21                 ObjectContext objectContext = ((IObjectContextAdapter)context).ObjectContext;
 22                 StorageMappingItemCollection mappingItemCollection = (StorageMappingItemCollection)objectContext.ObjectStateManager
 23                     .MetadataWorkspace.GetItemCollection(DataSpace.CSSpace);
 24                 mappingItemCollection.GenerateViews(new List<EdmSchemaError>());
 25             } 
 26         }
 27     }
 28 
 29     /// <summary>
 30     /// 自动迁移配置
 31     /// </summary>
 32     /// <typeparam name="TContext"></typeparam>
 33     public class AutoMigrationsConfiguration<TContext> : DbMigrationsConfiguration<TContext>
 34         where TContext : DbContext
 35     {
 36         /// <summary>
 37         /// 初始化一个<see cref="AutoMigrationsConfiguration{TContext}"/>类型的新实例
 38         /// </summary>
 39         public AutoMigrationsConfiguration()
 40         {
 41             AutomaticMigrationsEnabled = true;
 42             AutomaticMigrationDataLossAllowed = true;
 43             ContextKey = typeof(TContext).FullName;
 44         }
 45     }
 46 
 47     /// <summary>
 48     /// 实体注册Helper接口
 49     /// </summary>
 50     public interface IEntityRegstHelper
 51     {
 52         void RegTo(ConfigurationRegistrar confRegistrar);
 53     }
 54 
 55     // 客户
 56     public class Customer
 57     {
 58         public int CustomerID { get; set; }
 59         public String CustomerName { get; set; }
 60         public string Address { get; set; }
 61     }
 62     // 客户实体的注册Helper
 63     public class CustomerRegstHelper : IEntityRegstHelper
 64     {
 65         public void RegTo(ConfigurationRegistrar confRegistrar)
 66         {
 67             confRegistrar.Add<Customer>(new EntityTypeConfiguration<Customer>());
 68         }
 69     }
 70 
 71     public class Cus
 72     {
 73         public int CusID { get; set; }
 74         public String CusName { get; set; }
 75         public string Add { get; set; }
 76     }
 77     // 客户实体的注册Helper
 78     public class CusRegstHelper : IEntityRegstHelper
 79     {
 80         public void RegTo(ConfigurationRegistrar confRegistrar)
 81         {
 82             confRegistrar.Add<Cus>(new EntityTypeConfiguration<Cus>());
 83         }
 84     }
 85 
 86 
 87     public class MyDbContext : DbContext
 88     {
 89         public MyDbContext()
 90             : this(false)
 91         { }
 92 
 93         public MyDbContext(bool proxyCreationEnabled)
 94             : base("name=RongziCmsDbContext")
 95         {
 96             //The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer'
 97             //for the 'System.Data.SqlClient' ADO.NET provider could not be loaded. 
 98             //Make sure the provider assembly is available to the running application. 
 99             //See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.
100             var _ = System.Data.Entity.SqlServer.SqlProviderServices.Instance;
101             this.Configuration.ProxyCreationEnabled = proxyCreationEnabled;
102         }
103 
104         List<IEntityRegstHelper> entityRegstHelperlist;
105         // 添加实体注册
106         public void AddEntityRegHelper(IEntityRegstHelper r)
107         {
108             if (entityRegstHelperlist == null)
109                 entityRegstHelperlist = new List<IEntityRegstHelper>();
110             entityRegstHelperlist.Add(r);
111         }
112 
113         //public DbSet<Customer> Customers { get; set; }
114         protected override void OnModelCreating(DbModelBuilder modelBuilder)
115         {
116             // 动态地加入实体
117             if (entityRegstHelperlist != null)
118             {
119                 foreach (IEntityRegstHelper r in entityRegstHelperlist)
120                     r.RegTo(modelBuilder.Configurations);
121             }
122         }
123     }
View Code
原文地址:https://www.cnblogs.com/Hai--D/p/5416634.html