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. JACKSON JSON 操作帮助类

    一. 引入POM <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-li ...

  2. Python 数据驱动ddt 使用

    准备工作: pip install ddt 知识点: 一,数据驱动和代码驱动: 数据驱动的意思是  根据你提供的数据来测试的  比如 ATP框架 需要excel里面的测试用例 代码驱动是必须得写代码  ...

  3. shell脚本之函数的使用

    把代码封装成函数,相当于造了一个“轮子”,之后就直接重复使用即可. 函数的创建 shell中函数的创建有2种方式 1.使用function关键字 语法 function test { ... } 2. ...

  4. 每日英语:The First Day On A Job Is Tough Work

    Why is the first day on the job often the worst? New employees tend to be greeted with stacks of ben ...

  5. renderer:function参数介绍

    转载自:http://blog.sina.com.cn/s/blog_9eaf28f90101b7y3.html renderer:function(value, cellmeta, record, ...

  6. PgSQl临时表的创建

    创建前可先删除 drop table tmp0 创建临时表 select * into temp table tmp0 from xxx create index idx_tmp0_inner_cd ...

  7. Mybatis批量更新<转>

    Mybatis批量更新 批量操作就不进行赘述了.减少服务器与数据库之间的交互.网上有很多关于批量插入还有批量删除的帖子.但是批量更新却没有详细的解决方案. 实现目标 这里主要讲的是1张table中.根 ...

  8. Maven + Spring 进行多环境自动切换功能

    在pom.xml的<project></project>的最下放写入如下代码: <!-- profiles setting start [mvn install -P x ...

  9. WEB 项目中JAVA取得WEBROOT物理路径

    http://wwwzhouhui.iteye.com/blog/504330 ———————————————————————————————————————————————————————————— ...

  10. python中的字典 和 集合

    python中字典是一种key-value的数据类型 字典的特性: 1.无序的 2.key必须的唯一的,so,字典天生去重 语法: 增加 修改 删除 查找 多级字典嵌套及操作 字典的其他用法 #set ...