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

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

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

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

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

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

接口IStartupTask的實(shí)體類EfStartUpTask的實(shí)現(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ù)庫(kù)。

IDbContext,NopObjectContext系統(tǒng)數(shù)據(jù)庫(kù)操作上下文。加載所有數(shù)據(jù)庫(kù)映射類: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)啟動(dòng)時(shí)調(diào)用,建立數(shù)據(jù)表與實(shí)體的對(duì)應(yīng)關(guān)系。

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

//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 的實(shí)體類SqlServerDataProvider的數(shù)據(jù)庫(kù)初始化方法如下:

/// 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框架,通過(guò)數(shù)據(jù)庫(kù)訪問(wèn)上下文建立與數(shù)據(jù)庫(kù)的連接及實(shí)體與數(shù)據(jù)表的對(duì)應(yīng)廣西。并通過(guò)創(chuàng)建IRepositoryT>的泛型實(shí)體類來(lái)實(shí)現(xiàn)對(duì)每一種數(shù)據(jù)的處理,也就是所謂的Dao層。業(yè)務(wù)邏輯層通過(guò)每種實(shí)體的數(shù)據(jù)訪問(wèn)倉(cāng)庫(kù)RepositoryT>來(lái)進(jìn)行數(shù)據(jù)庫(kù)操作。

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

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

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