5

I have two contexts working with visual studio 2013. The one for the IdentityModel and another context I created for the main database. The problem I am having is whenever I run the update-database in the package manager console, it creates the ApplicationDBContext DB in both. The one for my main database never gets created because it seems it sees it as the ApplicationDBContext. I have tried Enable-Migrations -ContextTypeName DI_MVC.Models.DIDBContext and it does not create the main database tables for the DIDBContext class. I was able to do this in visual studio 2012 without an issue in creating the database. That was without the ApplicationDBContext though. Any help on what I am doing wrong would be greatly appreciated.

Below are my 2 code files. The normal IdentityModel and my DIDBContext class I created. Also I will display the web.config file as well with the database connections. There are 2 .mdf files that get created within the App_Data folder but both databases contain the membership tables.

using System;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration.Conventions;

namespace DI_MVC.Models
{
    public class DIDBContext : DbContext
    {
        public DIDBContext()
            : base("DIDBContext")
        {

        }

        public DbSet<DI_ConnectionStrings_CSI> CSIs { get; set; }
        public DbSet<DI_DropDownValues_DDV> DDVs { get; set; }
        public DbSet<DI_Types_DIT> DITs { get; set; }
        public DbSet<DI_FORM_DIF> DIFs { get; set; }
        public DbSet<DI_FormTabs_DFT> DFTs { get; set; }
        public DbSet<DFI_FormSections_DFS> DFSs { get; set; }
        public DbSet<DI_FormElements_DFE> DFEs { get; set; }
        public DbSet<DI_DFE_DFS_DDD> DDDs { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<DI_DFE_DFS_DDD>().HasKey(k => new { k.DDD_DFS_ID, k.DDD_DFE_ID });
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }
    }
}


using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration.Conventions;

namespace DI_MVC.Models
{
    // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
    public class ApplicationUser : IdentityUser
    {
        public DateTime BirthDate { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("DefaultConnection")
        {

        }
    }

}
<connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-DI_MVC-meohmeohmy.mdf;Initial Catalog=aspnet-DI_MVC-meohmeohmy;Integrated Security=True"
      providerName="System.Data.SqlClient" />
    <add name="DIDBContext" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-DI_MVC-MainDB.mdf;Initial Catalog=aspnet-DI_MVC-MainDB;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
tereško
  • 58,060
  • 25
  • 98
  • 150
  • Another thing to mention. The initial migration works, whether I do the ApplicationDbContext or the DIDBContext. It is the second migration that seems to be the issue. Every time I try to force a new migration context type, it continually tells me the previous migration is still pending even when I have already run the update-database. "Unable to generate an explicit migration because the following explicit migrations are pending: [201311300033115_InitialDIDBCreate]. Apply the pending explicit migrations before attempting to generate a new explicit migration." – PaperBeatsRock-PFFFT Nov 30 '13 at 00:37
  • I got it to work but I'm not too thrilled with how. I basically just removed the 201311300033115_InitialIDBCreate from the project and the error no longer appeared. It ran the update without a problem. Each update and migration I applied the -ConfigurationTypeName parameter setting it to a specific configuration class for each database context. That wasn't the answer either. Hopefully someone, maybe even myself, will have a much better resolution to this. – PaperBeatsRock-PFFFT Nov 30 '13 at 00:59
  • Helpful links are listed below: [http://stackoverflow.com/questions/13469881/how-do-i-enable-ef-migrations-for-multiple-contexts-to-separate-databases](13469881) [http://stackoverflow.com/questions/11679385/reset-entity-framework-migrations](11679385) – PaperBeatsRock-PFFFT Nov 30 '13 at 01:06

1 Answers1

0

You're not alone. It would make sense that if you enabled migrations for a single DbContext it would only update the DbContext that migrations are enabled for or at least let you specify which DbContext you wanted to update. However, that is not the case.

Read - this and this.

TLDR Enable-Migrations –EnableAutomaticMigrations

stink
  • 865
  • 8
  • 19