I am trying to create a basic dbcontext that contains all the shared objects that will always be reused in several projects, such as pages, users, roles, navigation, etc.
In doing so, I have a ContextBase class that inherits DbContext and defines all the necessary DbSets. Then I have a Context class that inherits ContextBase, where I define project-specific DbSets. Classes are defined as follows:
public class ContextBase : DbContext { public virtual DbSet<User> Users { get; set; }
In my global.asax:
Database.SetInitializer(new MigrateDatabaseToLatestVersion<Context, Configuration>());
where Configuration refers to a class that inherits from DbMigrationsConfiguration and overrides the Seed method.
Two context classes are defined in the same namespace, but intersect (so that I can update the base project in several existing projects without touching the project code) - I'm not sure if this is relevant.
MY PROBLEM: When running this code, it works fine, but when searching the database, it actually creates two different databases! One containing all the base entities, and one containing the base and user BOTH tables. CRUD operations are only performed in the user version (which is obviously what I want), but why does it also create another one?
Any help is appreciated, thanks!
UPDATE:
The following code is what I came across. It is not perfect, but it works. I would still like to receive feedback on how to improve this, but nonetheless, I hope this helps the further process. I REALLY DO NOT RECOMMEND THIS! This is very error prone and very unpleasant to debug. I just post this to find out if there are any better ideas or implementations to achieve this.
One (but not the only) problem that still exists is that MVC views must be manually added to projects. I added it to the Nuget package, but it takes 2 to 3 hours to apply the nuget package with so many files when VS is connected to TFS. With some additional work and a custom view engine, views can be precompiled ( http://blog.davidebbo.com/2011/06/precompile-your-mvc-views-using.html ).
The solution is divided into projects of the basic structure and user projects (each category includes its own models and repository template). Framework projects are packaged in the Nuget package, and then installed in any custom projects, making it easy to add the general functions of any project, such as user management, role and permissions, content management, etc. (Often called a boiler room) any new projects. This allows you to transfer any improvements to the template in any existing user projects.
Custom database initializer:
public class MyMigrateDatabaseToLatestVersion : IDatabaseInitializer<Context> { public void InitializeDatabase(Context context) {
DB Database Migration Configuration:
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) {
Configuration of custom DB projects:
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) {
Custom dbcontext
DbContext Frame:
//again nothing special public class ContextBase: DbContext { //example DbSet's public virtual DbSet<Models.User> Users { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder); }
In global.asax AppStart:
In the web.config file:
<connectionStrings> <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>