Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of EntityFramework Database pilot and data Operation in NopCommerce

2025-03-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

The purpose of this article is to share with you the content of the sample analysis of EntityFramework database pilot and data manipulation in NopCommerce. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

When the system starts, it executes the task: IStartupTask, and the main task is initializing and loading the database.

IStartupTask calls IEfDataProvider to initialize the database.

IEfDataProvider,SqlCeDataProvider: get data connection factory, different type of database, connection factory is different.

The implementation of the entity class EfStartUpTask of interface IStartupTask is as follows:

Public class EfStartUpTask: IStartupTask {public void Execute () {var settings = EngineContext.Current.Resolve (); if (settings! = null & & settings.IsValid ()) {var provider = EngineContext.Current.Resolve (); 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 initializes the database.

IDbContext,NopObjectContext system database operation context. Load all database mapping classes: EntityTypeConfiguration. The code is as follows:

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);}

This method is inherited from DbContext. It is called when the system is started to establish the corresponding relationship between the data table and the entity.

The creation of the database factory and the loading of the database are realized in the type dependency registration class Nop.Web.Framework.DependencyRegistrar. The code is as follows:

/ / data layer var dataSettingsManager = new DataSettingsManager (); var dataProviderSettings = dataSettingsManager.LoadSettings (); builder.Register (c = > dataSettingsManager.LoadSettings ()). As (); builder.Register (x = > new EfDataProviderManager (x.Resolve ()). As (). InstancePerDependency (); builder.Register (x = > (IEfDataProvider) x.Resolve (). LoadDataProvider ()). As (). InstancePerDependency (); builder.Register (x = > (IEfDataProvider) x.Resolve (). LoadDataProvider ()). As (). InstancePerDependency () If (dataProviderSettings! = null & & dataProviderSettings.IsValid ()) {var efDataProviderManager = new EfDataProviderManager (dataSettingsManager.LoadSettings ()); var dataProvider = (IEfDataProvider) efDataProviderManager.LoadDataProvider (); dataProvider.InitConnectionFactory (); builder.Register (c = > new NopObjectContext (dataProviderSettings.DataConnectionString)). InstancePerHttpRequest ();} else {builder.Register (c = > new NopObjectContext (dataSettingsManager.LoadSettings () .DataConnectionString)) .InstancePerHttpRequest ();} builder.RegisterGeneric (typeof (EfRepository)) .as (typeof (IRepository)). InstancePerHttpRequest ()

The database initialization method of the entity class SqlServerDataProvider of API IEfDataProvider is as follows:

/ 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 ("~ / 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 (tablesToValidate, customCommands.ToArray ()); Database.SetInitializer (initializer);}

In addition, the EntityFramework skill is the ORM framework, which establishes the connection with the database and the correspondence between entities and data tables through the database access context. And through the creation of IRepository generic entity classes to achieve the processing of each kind of data, that is, the so-called Dao layer. The business logic layer operates the database through the data access warehouse Repository of each entity.

Thank you for reading! On the "NopCommerce EntityFramework database pilot and data operation example analysis" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, you can share it out for more people to see it!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report