系統(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ù)詳解