Search code examples
asp.netentity-framework-6asp.net-identity

ASP.Net Identity - Use custom Schema


I am using MVC5 + Ef6 code first with ASP.Net Identity 1.0 and wish to have the tables created in a custom schema. i.e. a schema that is not the dbo schema.

I reversed engineered my databse using the Ef power tools and set the schema name for all other tables in the mapping class to the following

this.ToTable("tableName", "schemaName");

I tried doing this for the ASP.Net tables but it kept giving me a lots of errors and eventually I gave up. If I exclude the (reverse engineered) ASP.Net Identity tables from my project they will be created but always in the dbo schema

Anyone know how to do this?


Solution

  • public class MyDbContext : EntityDbContext<ApplicationUser>
    {
        public DbSet<ApplicationUser> Users { get; set; }
    
        public MyDbContext() : base()
        {
        }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
    
            // You can globally assign schema here
            modelBuilder.HasDefaultSchema("schemaName");
        }
    }