您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關NopCommerce中EntityFramework數據庫初試化及數據操作的示例分析的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
系統啟動時執行任務:IStartupTask,啟動時執行的任務主要是數據庫的初始化和加載。
IStartupTask調用IEfDataProvider進行數據庫的初始化。
IEfDataProvider,SqlCeDataProvider:獲取數據連接工廠,不同類型數據庫,連接工廠不同。
接口IStartupTask的實體類EfStartUpTask的實現如下:
public class EfStartUpTask : IStartupTask { public void Execute() { var settings = EngineContext.Current.Resolve<DataSettings>(); if (settings != null && settings.IsValid()) { var provider = EngineContext.Current.Resolve<IEfDataProvider>(); 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初始化數據庫。
IDbContext,NopObjectContext系統數據庫操作上下文。加載所有數據庫映射類:EntityTypeConfiguration<TEntityType>。代碼如下:
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。并在系統啟動時調用,建立數據表與實體的對應關系。
在類型依賴注冊類Nop.Web.Framework.DependencyRegistrar中實現數據庫工廠的創建、數據庫的加載。如下代碼:
//data layer var dataSettingsManager = new DataSettingsManager(); var dataProviderSettings = dataSettingsManager.LoadSettings(); builder.Register(c => dataSettingsManager.LoadSettings()).As<DataSettings>(); builder.Register(x => new EfDataProviderManager(x.Resolve<DataSettings>())).As<BaseDataProviderManager>().InstancePerDependency(); builder.Register(x => (IEfDataProvider)x.Resolve<BaseDataProviderManager>().LoadDataProvider()).As<IDataProvider>().InstancePerDependency(); builder.Register(x => (IEfDataProvider)x.Resolve<BaseDataProviderManager>().LoadDataProvider()).As<IEfDataProvider>().InstancePerDependency(); if (dataProviderSettings != null && dataProviderSettings.IsValid()) { var efDataProviderManager = new EfDataProviderManager(dataSettingsManager.LoadSettings()); var dataProvider = (IEfDataProvider)efDataProviderManager.LoadDataProvider(); dataProvider.InitConnectionFactory(); builder.Register<IDbContext>(c => new NopObjectContext(dataProviderSettings.DataConnectionString)).InstancePerHttpRequest(); } else { builder.Register<IDbContext>(c => new NopObjectContext(dataSettingsManager.LoadSettings().DataConnectionString)).InstancePerHttpRequest(); } builder.RegisterGeneric(typeof(EfRepository<>)).As(typeof(IRepository<>)).InstancePerHttpRequest();
接口IEfDataProvider 的實體類SqlServerDataProvider的數據庫初始化方法如下:
/// <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 List<string>(); //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 CreateTablesIfNotExist<NopObjectContext>(tablesToValidate, customCommands.ToArray()); Database.SetInitializer(initializer); }
另外,EntityFramework本事是ORM框架,通過數據庫訪問上下文建立與數據庫的連接及實體與數據表的對應廣西。并通過創建IRepository<T>的泛型實體類來實現對每一種數據的處理,也就是所謂的Dao層。業務邏輯層通過每種實體的數據訪問倉庫Repository<T>來進行數據庫操作。
感謝各位的閱讀!關于“NopCommerce中EntityFramework數據庫初試化及數據操作的示例分析”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。