DBContent初始化 —— 关联Entity查找

一、      关联到具体的Entity

二、      通过EntityTypeConfiguration 关联到DbContent

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

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

五、      查找当前DbContent的Entity 

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

需实现DbContextInitializerBase基类

筛选出当前DbContent的Entity

初始化配置

DBContent初始化 —— DBContent生成

一、      主要方法

二、      框架初始化入口

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

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

附一段单元测试代码

 public class DbContentInitializeTest
{
[Fact()]
public void DbContentInitialize_Test()
{
using (var context = new MyDbContext())
{
context.AddEntityRegHelper(new CustomerRegstHelper());
context.AddEntityRegHelper(new CusRegstHelper());
IDatabaseInitializer<MyDbContext> initializer;
if (!context.Database.Exists())
{
initializer = new CreateDatabaseIfNotExists<MyDbContext>(); ;
}
else
{
initializer = new MigrateDatabaseToLatestVersion<MyDbContext, AutoMigrationsConfiguration<MyDbContext>>(); ;
}
Database.SetInitializer(initializer); ObjectContext objectContext = ((IObjectContextAdapter)context).ObjectContext;
StorageMappingItemCollection mappingItemCollection = (StorageMappingItemCollection)objectContext.ObjectStateManager
.MetadataWorkspace.GetItemCollection(DataSpace.CSSpace);
mappingItemCollection.GenerateViews(new List<EdmSchemaError>());
}
}
} /// <summary>
/// 自动迁移配置
/// </summary>
/// <typeparam name="TContext"></typeparam>
public class AutoMigrationsConfiguration<TContext> : DbMigrationsConfiguration<TContext>
where TContext : DbContext
{
/// <summary>
/// 初始化一个<see cref="AutoMigrationsConfiguration{TContext}"/>类型的新实例
/// </summary>
public AutoMigrationsConfiguration()
{
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
ContextKey = typeof(TContext).FullName;
}
} /// <summary>
/// 实体注册Helper接口
/// </summary>
public interface IEntityRegstHelper
{
void RegTo(ConfigurationRegistrar confRegistrar);
} // 客户
public class Customer
{
public int CustomerID { get; set; }
public String CustomerName { get; set; }
public string Address { get; set; }
}
// 客户实体的注册Helper
public class CustomerRegstHelper : IEntityRegstHelper
{
public void RegTo(ConfigurationRegistrar confRegistrar)
{
confRegistrar.Add<Customer>(new EntityTypeConfiguration<Customer>());
}
} public class Cus
{
public int CusID { get; set; }
public String CusName { get; set; }
public string Add { get; set; }
}
// 客户实体的注册Helper
public class CusRegstHelper : IEntityRegstHelper
{
public void RegTo(ConfigurationRegistrar confRegistrar)
{
confRegistrar.Add<Cus>(new EntityTypeConfiguration<Cus>());
}
} public class MyDbContext : DbContext
{
public MyDbContext()
: this(false)
{ } public MyDbContext(bool proxyCreationEnabled)
: base("name=RongziCmsDbContext")
{
//The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer'
//for the 'System.Data.SqlClient' ADO.NET provider could not be loaded.
//Make sure the provider assembly is available to the running application.
//See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.
var _ = System.Data.Entity.SqlServer.SqlProviderServices.Instance;
this.Configuration.ProxyCreationEnabled = proxyCreationEnabled;
} List<IEntityRegstHelper> entityRegstHelperlist;
// 添加实体注册
public void AddEntityRegHelper(IEntityRegstHelper r)
{
if (entityRegstHelperlist == null)
entityRegstHelperlist = new List<IEntityRegstHelper>();
entityRegstHelperlist.Add(r);
} //public DbSet<Customer> Customers { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// 动态地加入实体
if (entityRegstHelperlist != null)
{
foreach (IEntityRegstHelper r in entityRegstHelperlist)
r.RegTo(modelBuilder.Configurations);
}
}
}

OSharp DbContent初始化分析的更多相关文章

  1. JVM学习四:JVM之类加载器之初始化分析

    在经过了前面的加载  和 连接分析之后,这一节我们进入重要的初始化分析过程: 一.认识初始化 初始化:这个似乎与上面的初始化为默认值有点矛盾,我们再看一遍:为累的静态变量赋予正确的初始值,上面是赋予默 ...

  2. sparkContext之一:sparkContext的初始化分析

    Spark源码学习:sparkContext的初始化分析 spark可以运行在本地模式local下,可以运行在yarn和standalone模式下,但是本地程序是通过什么渠道和这些集群交互的呢?那就是 ...

  3. Ztack学习笔记(2)-系统初始化分析

    main函数先执行初始化工作,包括硬件.网络层.任务等的初始化. 一 系统初始化 系统初始化函数主要完成内存分配.消息队列头.定时器.电源管理.任务系统及内存栈等的初始化,具体如下代码所示: //os ...

  4. cc2530操作任务系统初始化分析

    操作系统任务初始化void osalInitTasks( void ){ uint8 taskID = 0; // 分配内存,返回指向缓冲区的指针 tasksEvents = (uint16 *)os ...

  5. Android 5.0 Phone初始化分析

    已经更新至个人blog:http://dxjia.cn/2015/07/android-5-0-phone-init-analysis/ persistent属性 要想了解phone的框架,首先需要了 ...

  6. socket相关的开机初始化分析

    针对内核3.9 系统开启时,会使用init/main.c,然后再里面调用kernel_init(),在里面会再调用do_basic_setup(),调用do_initcalls(),调用do_one_ ...

  7. STM32_3 时钟初始化分析

    在startup文件中,调用了2个函数,一个是System_Init, 另一个是main. System_Init()在system_stm32f10x.c 这个文件中,先看一下时钟树,再分析一下这个 ...

  8. Android编译系统环境过程初始化分析【转】

    本文转载自:http://blog.csdn.net/luoshengyang/article/details/18928789 Android源代码在编译之前,要先对编译环境进行初始化,其中最主要就 ...

  9. 列表初始化 分析initializer_list<T>的实现

    列表初始化(1)_统一初始化 1. 统一初始化(Uniform Initialization) (1)在C++11之前,很多程序员特别是初学者对如何初始化一个变量或对象的问题很容易出现困惑.因为可以用 ...

随机推荐

  1. Android Shape 形状

    Shape继承体系: Shape (android.graphics.drawable.shapes) ----PathShape (android.graphics.drawable.shapes) ...

  2. Jquery 事件执行两次

    js(jquery)的on绑定点击事件执行两次的解决办法—不是事件绑定而是事件冒泡 阻止冒泡的方法并不止 return false 这一种,还有event.stopPropagation(),这两种方 ...

  3. cocos2dx对所有子节点设置透明度

    看到cocos2dx2.2.5发布了,修复了输入框的bug,于是我们的项目也升级到了2.2.5, 升级过程还是比较顺利,没想到后来发现设置透明度无效了. 经过调试发现要调用一下setCascadeOp ...

  4. HTML中label的两种使用方法

    如果您在 label 元素内点击文本,就会触发此控件.就是说,当用户选择该标签时,浏览器就会自动将焦点转到和标签相关的表单控件上. 有两种使用方法: 方法1: <label for=" ...

  5. C语言 · 素数判断

     算法提高 素数判断   时间限制:1.0s   内存限制:512.0MB      编写一函数IsPrime,判断某个大于2的正整数是否为素数. 样例输入: 5样例输出:yes 样例输入: 9样例输 ...

  6. xeno 实时性能测试 系统时钟1秒100个tick再测试

    root@sama5d3-linux:/usr/bin ./latency -t0 -T25 -p100                            == Sampling period: ...

  7. 使用Navicat连接阿里云服务器上的MySQL数据库=======Linux 开放 /etc/hosts.allow

    使用Navicat连接阿里云服务器上的MySQL数据库   1.首先打开Navicat,文件>新建连接> 2,两张连接方法 1>常规中输入数据库的主机名,端口,用户名,密码 这种直接 ...

  8. SQL简明教程系列15 创建索引

    CREATE INDEX用于在表中创建索引. 索引使数据库应用程序可以更快地查找数据. 注:更新一个包含索引的表比更新一个没有索引的表更多的时间,这是由于索引本身也需要更新.因此,理想的做法是仅仅在常 ...

  9. C++ 类的深拷贝和浅拷贝完美解决

    //类的深拷贝和浅拷贝 #define _CRT_SECURE_NO_WARNINGS #include<iostream> using namespace std; class Poin ...

  10. JDBC中,用于表示数据库连接的对象是。(选择1项)

    JDBC中,用于表示数据库连接的对象是.(选择1项) A.Statement B.Connection C. DriverManager D.PreparedStatement 解答:B