我正在尝试创建一个基本 dbcontext,其中包含将始终在多个项目中重用的所有常见实体,如页面、用户、角色、导航等。
在这样做时,我有一个 ContextBase 类,它继承了 DbContext 并定义了我想要的所有 DbSet。然后我有一个继承 ContextBase 的 Context 类,我在其中定义了项目特定的 DbSet。类定义如下:
public class ContextBase : DbContext
{
public virtual DbSet<User> Users { get; set; }
//more sets
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new UsersConfiguration());
//add more configurations
}
}
public class Context : ContextBase
{
public DbSet<Building> Buildings { get; set; }
//some more project specific sets
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Configurations.Add(new BuildingsConfiguration());
//add more project specific configs
}
}
在我的 global.asax 中:
Database.SetInitializer(new MigrateDatabaseToLatestVersion<Context, Configuration>());
其中 Configuration 是指继承 DbMigrationsConfiguration 并覆盖 Seed 方法的类。
这两个上下文类在同一个命名空间中定义,但跨程序集(以便我可以在多个现有项目中更新基础项目而不触及项目特定代码) - 不确定这是否相关。
我的问题: 运行此代码时,它工作正常,但是在查看数据库时,它实际上创建了两个不同的数据库!一个包含所有基本实体表,另一个包含基本表和自定义表。CRUD 操作仅在自定义版本上执行(这显然是我想要的),但为什么它也会创建另一个模式?
任何帮助表示赞赏,谢谢!
更新:
以下代码是我最终得到的。这并不理想,但它确实有效。我仍然希望获得有关改进方法的反馈,但同时我希望这有助于进一步推进这一过程。我真的不建议这样做!调试起来非常容易出错并且非常令人沮丧。我只是发布这个,看看是否有更好的想法或实现来实现这一点。
仍然存在的一个(但不是唯一的)问题是必须手动将 MVC 视图添加到项目中。我已经将它添加到Nuget包中,但是当VS连接到TFS时,应用一个包含这么多文件的nuget包需要2到3个小时。通过更多工作和自定义视图引擎,可以预编译视图(http://blog.davidebbo.com/2011/06/precompile-your-mvc-views-using.html)。
该解决方案分为基本框架项目和自定义项目(每个类别都包括自己的模型和存储库模式)。框架项目打包在 Nuget 包中,然后安装在任何自定义项目中,从而可以轻松添加任何项目的通用功能,如用户、角色和权限管理、内容管理等(通常称为样板)任何新项目。这允许在任何现有的自定义项目中迁移样板的任何改进。
自定义数据库初始化器:
public class MyMigrateDatabaseToLatestVersion : IDatabaseInitializer<Context>
{
public void InitializeDatabase(Context context)
{
//create the base migrator
var baseConfig = new FrameworkConfiguration();
var migratorBase = new DbMigrator(baseConfig);
//create the custom migrator
var customConfig = new Configuration();
var migratorCustom = new DbMigrator(customConfig);
//now I need to check what migrations have not yet been applied
//and then run them in the correct order
if (migratorBase.GetPendingMigrations().Count() > 0)
{
try
{
migratorBase.Update();
}
catch (System.Data.Entity.Migrations.Infrastructure.AutomaticMigrationsDisabledException)
{
//if an error occured, the seed would not have run, so we run it again.
baseConfig.RunSeed(context);
}
}
if (migratorCustom.GetPendingMigrations().Count() > 0)
{
try
{
migratorCustom.Update();
}
catch (System.Data.Entity.Migrations.Infrastructure.AutomaticMigrationsDisabledException)
{
//if an error occured, the seed would not have run, so we run it again.
customConfig.RunSeed(context);
}
}
}
}
框架的数据库迁移配置:
public class FrameworkConfiguration: DbMigrationsConfiguration<Repository.ContextBase>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
}
public void RunSeed(Repository.ContextBase context)
{
Seed(context);
}
protected override void Seed(Repository.ContextBase context)
{
// This method will be called at every app start so it should use the AddOrUpdate method rather than just Add.
FrameworkDatabaseSeed.Seed(context);
}
}
自定义项目的数据库迁移配置:
public class Configuration : DbMigrationsConfiguration<Repository.Context>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
}
public void RunSeed(Repository.Context context)
{
Seed(context);
}
protected override void Seed(Repository.Context context)
{
// This method will be called at every app start so it should use the AddOrUpdate method rather than just Add.
CustomDatabaseSeed.Seed(context);
}
}
自定义 DbContext
//nothing special here, simply inherit ContextBase, IContext interface is purely for DI
public class Context : ContextBase, IContext
{
//Add the custom DBsets, i.e.
public DbSet<Chart> Charts { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
//Assign the model configs, i.e.
modelBuilder.Configurations.Add(new ChartConfiguration());
}
}
框架 DbContext:
//again nothing special
public class ContextBase: DbContext
{
//example DbSet's
public virtual DbSet<Models.User> Users { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder);
}
在 global.asax AppStart 中:
//first remove the base context initialiser
Database.SetInitializer<ContextBase>(null);
//set the inherited context initializer
Database.SetInitializer(new MyMigrateDatabaseToLatestVersion());
在 web.config 中:
<connectionStrings>
<!--put the exact same connection string twice here and name it the same as the base and overridden context. That way they point to the same database. -->
<add name="Context" connectionString="Data Source=.\SQLEXPRESS; Initial Catalog=CMS2013; Integrated Security=SSPI;MultipleActiveResultSets=true;" providerName="System.Data.SqlClient"/>
<add name="ContextBase" connectionString="Data Source=.\SQLEXPRESS; Initial Catalog=CMS2013; Integrated Security=SSPI;MultipleActiveResultSets=true;" providerName="System.Data.SqlClient"/>
</connectionStrings>