学者谷

IStartupTask连接数据库的方法

系统启动时执行任务:IStartupTask,启动时执行的任务主要是数据库的初始化和加载。

IStartupTask连接数据库的方法

IStartupTask调用IEfDataProvider进行数据库的初始化。

IEfDataProvider,SqlCeDataProvider:获取数据连接工厂,不同类型数据库,连接工厂不同。

接口IStartupTask的.实体类EfStartUpTask的实现如下:

public class EfStartUpTask : IStartupTask { public void Execute() { var settings = lve(); if (settings != null && lid()) { var provider = lve(); if (provider == null) throw new NopException("No EfDataProvider found"); atabaseInitializer(); } } public int Order { //ensure that this task is run first get { return -1000; } } }

SqlCeInitializer,CreateCeDatabaseIfNotExists初始化数据库。

IDbContext,NopObjectContext系统数据库操作上下文。加载所有数据库映射类:EntityTypeConfiguration。代码如下:

protected override void OnModelCreating(DbModelBuilder modelBuilder) { //dynamically load all configuration // configType = typeof(LanguageMap); //any of your configuration classes here //var typesToRegister = ssembly(configType)ypes() var typesToRegister = xecutingAssembly()ypes() e(type => !llOrEmpty(space)) e(type => Type != null && nericType && enericTypeDefinition() == typeof(EntityTypeConfiguration<>)); foreach (var type in typesToRegister) { dynamic configurationInstance = teInstance(type); (configurationInstance); } // do it manually below. For example, //(new LanguageMap()); delCreating(modelBuilder); }

此方法是继承自DbContext。并在系统启动时调用,建立数据表与实体的对应关系。

在类型依赖注册类ndencyRegistrar中实现数据库工厂的创建、数据库的加载。如下代码:

//data layer var dataSettingsManager = new DataSettingsManager(); var dataProviderSettings = Settings(); ster(c => Settings())(); ster(x => new EfDataProviderManager(lve()))()ancePerDependency(); ster(x => (IEfDataProvider)lve()DataProvider())()ancePerDependency(); ster(x => (IEfDataProvider)lve()DataProvider())()ancePerDependency(); if (dataProviderSettings != null && lid()) { var efDataProviderManager = new EfDataProviderManager(Settings()); var dataProvider = (IEfDataProvider)DataProvider(); ConnectionFactory(); ster(c => new NopObjectContext(ConnectionString))ancePerHttpRequest(); } else { ster(c => new NopObjectContext(Settings()ConnectionString))ancePerHttpRequest(); } sterGeneric(typeof(EfRepository<>))(typeof(IRepository<>))ancePerHttpRequest();

接口IEfDataProvider 的实体类SqlServerDataProvider的数据库初始化方法如下

///

/// Set database initializer ///

public override void SetDatabaseInitializer() { //pass some table names to ensure that we have nopCommerce 2.X installed var tablesToValidate = new[] {"Customer", "Discount", "Order", "Product", "ShoppingCartItem"}; //custom commands (stored proedures, indexes) var customCommands = new List

(); //use webHelper.MapPath instead of HostingEnvironment.MapPath which is not available in unit tests customCommands.AddRange(ParseCommands(HostingEnvironment.MapPath("~/App_Data/"), false)); //use webHelper.MapPath instead of HostingEnvironment.MapPath which is not available in unit tests customCommands.AddRange(ParseCommands(HostingEnvironment.MapPath("~/App_Data/"), false)); var initializer = new CreateTablesIfNotExist(tablesToValidate, ray()); nitializer(initializer); }

另外,EntityFramework本事是ORM框架,通过数据库访问上下文建立与数据库的连接及实体与数据表的对应广西。并通过创建IRepository的泛型实体类来实现对每一种数据的处理,也就是所谓的Dao层。业务逻辑层通过每种实体的数据访问仓库Repository来进行数据库操作。