主頁 > 知識庫 > NopCommerce架構(gòu)分析之(三)EntityFramework數(shù)據(jù)庫初試化及數(shù)據(jù)操作

NopCommerce架構(gòu)分析之(三)EntityFramework數(shù)據(jù)庫初試化及數(shù)據(jù)操作

熱門標(biāo)簽:代理接電話機(jī)器人如何取消 地圖標(biāo)注專業(yè)團(tuán)隊 400電話辦理哪家性價比高 塔城代理外呼系統(tǒng) 濮陽外呼電銷系統(tǒng)怎么樣 天心智能電銷機(jī)器人 地圖標(biāo)注的公司有哪些 地圖定位圖標(biāo)標(biāo)注 遂寧市地圖標(biāo)注app

系統(tǒng)啟動時執(zhí)行任務(wù):IStartupTask,啟動時執(zhí)行的任務(wù)主要是數(shù)據(jù)庫的初始化和加載。

IStartupTask調(diào)用IEfDataProvider進(jìn)行數(shù)據(jù)庫的初始化。

IEfDataProvider,SqlCeDataProvider:獲取數(shù)據(jù)連接工廠,不同類型數(shù)據(jù)庫,連接工廠不同。

接口IStartupTask的實體類EfStartUpTask的實現(xiàn)如下:

public class EfStartUpTask : IStartupTask 
{ 
 public void Execute() 
 { 
  var settings = EngineContext.Current.ResolveDataSettings>(); 
  if (settings != null  settings.IsValid()) 
  { 
   var provider = EngineContext.Current.ResolveIEfDataProvider>(); 
   if (provider == null) 
    throw new NopException("No EfDataProvider found"); 
   provider.SetDatabaseInitializer(); 
  } 
 } 

 public int Order 
 { 
  //ensure that this task is run first 
  get { return -1000; } 
 } 
}

SqlCeInitializer,CreateCeDatabaseIfNotExists初始化數(shù)據(jù)庫。

IDbContext,NopObjectContext系統(tǒng)數(shù)據(jù)庫操作上下文。加載所有數(shù)據(jù)庫映射類:EntityTypeConfigurationTEntityType>。代碼如下:

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
{ 
 //dynamically load all configuration 
 //System.Type configType = typeof(LanguageMap); //any of your configuration classes here 
 //var typesToRegister = Assembly.GetAssembly(configType).GetTypes() 

 var typesToRegister = Assembly.GetExecutingAssembly().GetTypes() 
 .Where(type => !String.IsNullOrEmpty(type.Namespace)) 
 .Where(type => type.BaseType != null  type.BaseType.IsGenericType  type.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration>)); 
 foreach (var type in typesToRegister) 
 { 
 dynamic configurationInstance = Activator.CreateInstance(type); 
 modelBuilder.Configurations.Add(configurationInstance); 
 } 
 //...or do it manually below. For example, 
 //modelBuilder.Configurations.Add(new LanguageMap()); 



 base.OnModelCreating(modelBuilder); 
} 

此方法是繼承自DbContext。并在系統(tǒng)啟動時調(diào)用,建立數(shù)據(jù)表與實體的對應(yīng)關(guān)系。

在類型依賴注冊類Nop.Web.Framework.DependencyRegistrar中實現(xiàn)數(shù)據(jù)庫工廠的創(chuàng)建、數(shù)據(jù)庫的加載。如下代碼:

//data layer 
var dataSettingsManager = new DataSettingsManager(); 
var dataProviderSettings = dataSettingsManager.LoadSettings(); 
builder.Register(c => dataSettingsManager.LoadSettings()).AsDataSettings>(); 
builder.Register(x => new EfDataProviderManager(x.ResolveDataSettings>())).AsBaseDataProviderManager>().InstancePerDependency(); 


builder.Register(x => (IEfDataProvider)x.ResolveBaseDataProviderManager>().LoadDataProvider()).AsIDataProvider>().InstancePerDependency(); 
builder.Register(x => (IEfDataProvider)x.ResolveBaseDataProviderManager>().LoadDataProvider()).AsIEfDataProvider>().InstancePerDependency(); 

if (dataProviderSettings != null  dataProviderSettings.IsValid()) 
{ 
 var efDataProviderManager = new EfDataProviderManager(dataSettingsManager.LoadSettings()); 
 var dataProvider = (IEfDataProvider)efDataProviderManager.LoadDataProvider(); 
 dataProvider.InitConnectionFactory(); 

 builder.RegisterIDbContext>(c => new NopObjectContext(dataProviderSettings.DataConnectionString)).InstancePerHttpRequest(); 
} 
else 
{ 
 builder.RegisterIDbContext>(c => new NopObjectContext(dataSettingsManager.LoadSettings().DataConnectionString)).InstancePerHttpRequest(); 
} 


builder.RegisterGeneric(typeof(EfRepository>)).As(typeof(IRepository>)).InstancePerHttpRequest(); 

接口IEfDataProvider 的實體類SqlServerDataProvider的數(shù)據(jù)庫初始化方法如下:

/// summary> 
/// Set database initializer 
/// /summary> 
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 Liststring>(); 
 //use webHelper.MapPath instead of HostingEnvironment.MapPath which is not available in unit tests 
 customCommands.AddRange(ParseCommands(HostingEnvironment.MapPath("~/App_Data/SqlServer.Indexes.sql"), false)); 
 //use webHelper.MapPath instead of HostingEnvironment.MapPath which is not available in unit tests 
 customCommands.AddRange(ParseCommands(HostingEnvironment.MapPath("~/App_Data/SqlServer.StoredProcedures.sql"), false)); 
 
 var initializer = new CreateTablesIfNotExistNopObjectContext>(tablesToValidate, customCommands.ToArray()); 
 Database.SetInitializer(initializer); 
} 

另外,EntityFramework本事是ORM框架,通過數(shù)據(jù)庫訪問上下文建立與數(shù)據(jù)庫的連接及實體與數(shù)據(jù)表的對應(yīng)廣西。并通過創(chuàng)建IRepositoryT>的泛型實體類來實現(xiàn)對每一種數(shù)據(jù)的處理,也就是所謂的Dao層。業(yè)務(wù)邏輯層通過每種實體的數(shù)據(jù)訪問倉庫RepositoryT>來進(jìn)行數(shù)據(jù)庫操作。

您可能感興趣的文章:
  • 詳解如何在ASP.NET Core中應(yīng)用Entity Framework
  • EntityFramework 6.x學(xué)習(xí)之多個上下文遷移實現(xiàn)分布式事務(wù)詳解

標(biāo)簽:本溪 宜春 汕頭 麗江 重慶 河南 婁底 吉林

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《NopCommerce架構(gòu)分析之(三)EntityFramework數(shù)據(jù)庫初試化及數(shù)據(jù)操作》,本文關(guān)鍵詞  NopCommerce,架構(gòu),分析,之,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《NopCommerce架構(gòu)分析之(三)EntityFramework數(shù)據(jù)庫初試化及數(shù)據(jù)操作》相關(guān)的同類信息!
  • 本頁收集關(guān)于NopCommerce架構(gòu)分析之(三)EntityFramework數(shù)據(jù)庫初試化及數(shù)據(jù)操作的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章