最近因为项目需要,研究了下EF的读写分离,所以做了一个demo进行测试,下面是项目的结构
表现层view
主要提供Web、WebApi等表现层的解决方案
公共层public
主要提供项目公共类库,数据缓存基础方法等
实体层model
主要提供数据库映射模型,还有就是DDD领域操作模型
数据层Db
主要封装EF操作基础类
数据服务层Service
主要提供数据库操作服务、缓存操作服务
数据接口服务层inface
主要提供数据库操作服务接口、缓存操作服务接口
1.首先是多数据库的支持,目前就支持mysql/sqlservice,如果需要添加更多的数据库支持,只需要再数据库操作类型上面添加即可
数据库类型 DbContextType : byte { SqlService = 1, MySql = 2 }
View Code分别对mysql/sqlservice的上下文操作进行封装
MySql操作类 /// </summary> [DbConfigurationType(typeof(MySqlEFConfiguration))] public class MySqlContext : DbContext { public DbSet<Test> TestEntities { get; set; } 配置默认的字符串链接 MySqlContext() : ) { } 自定义数据库链接 MySqlContext(string connenction) : base(connenction) { } 实体对应规则的映射配置 OnModelCreating(DbModelBuilder modelBuilder) { } }
View Code
Sql数据库操作类 SqlServiceContext : DbContext { 配置默认的字符串链接 SqlServiceContext() { } 自定义数据库链接 SqlServiceContext(string connenction) : base(connenction) { } 实体对应规则的映射配置 OnModelCreating(DbModelBuilder modelBuilder) { } }
View Code在view调用时候,进行ef上下文初始化只需要设置类型
数据库策略初始化类 DBInitializer { public static DbContextType DbContextType { get; set; } 数据库初始化策略配置 Initialize(DbContextType ContextType) { ]; DbContextType = ContextType; (ContextType == DbContextType.SqlService) { Database.SetInitializer(new MigrateDatabaseToLatestVersion<WriteSqlServiceContext, WriteSqlServiceDBConfiguration>()); ) { Database.SetInitializer(new MigrateDatabaseToLatestVersion<ReadSqlServiceContext, ReadSqlSqlServiceDBConfiguration>()); } else { Database.SetInitializer<ReadSqlServiceContext>(null); } } else { Database.SetInitializer(new MigrateDatabaseToLatestVersion<WriteMySqlContext, WriteMySqlDBConfiguration>()); ) { Database.SetInitializer(new MigrateDatabaseToLatestVersion<ReadMySqlContext, ReadMySqlDBConfiguration>()); } else { Database.SetInitializer<ReadMySqlContext>(null); } //Database.SetInitializer<WriteMySqlContext>(null); // Database.SetInitializer<ReadMySqlContext>(null); } Database.SetInitializer(new DropCreateDatabaseIfModelChanges<ContextHelper>()); // Database.SetInitializer<ContextHelper>(new DropCreateDatabaseIfModelChanges<ContextHelper>()); } }
View Code
public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); //Autofac //ContainerBuilder builder = new ContainerBuilder(); //builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()); //IContainer container = builder.Build(); //DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); //Autofac初始化过程 ContainerBuilder builder = new ContainerBuilder(); builder.RegisterControllers(System.Reflection.Assembly.GetExecutingAssembly());assemblys = BuildManager.GetReferencedAssemblies().Cast<Assembly>().ToList(); builder.RegisterAssemblyTypes(assemblys.ToArray()).Where(t => t.Name.Contains()).AsImplementedInterfaces(); var container = builder.Build(); DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); //初始化数据库 DBInitializer.Initialize(DbContextType.MySql); } }
View Code通过上面多数据库的支持已经完成,下面进行读写分离,分别进行继承上述上下文操作