IStartupTask連線資料庫的方法
系統啟動時執行任務:IStartupTask,啟動時執行的任務主要是資料庫的初始化和載入。
IStartupTask呼叫IEfDataProvider進行資料庫的初始化。
IEfDataProvider,SqlCeDataProvider:獲取資料連線工廠,不同型別資料庫,連線工廠不同。
介面IStartupTask的.實體類EfStartUpTask的實現如下:
public class EfStartUpTask : IStartupTask { public void Execute() { var settings = lve
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())
介面IEfDataProvider 的實體類SqlServerDataProvider的資料庫初始化方法如下:
///
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
另外,EntityFramework本事是ORM框架,通過資料庫訪問上下文建立與資料庫的連線及實體與資料表的對應廣西。並通過建立IRepository