Search code examples
asp.net-mvcweb-configconnection-stringsql-server-expressasp.net-identity-2

Where is my MVC 5 code-first database created? (not using AttachDbFilename)


I have the following connection string in my MVC 5 weApp:

<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;User Instance=True;Initial Catalog=MatWebDB;Integrated Security=True" providerName="System.Data.SqlClient" />

I'm using Entity Framework code-first and Identity 2 for forms authentication.

Evreything seems to work fine: i register a new user and it logs in properly. The problem is that i cannot find the database that was created; in my SQL Server Management Studio there is no "MatWebDB" database created.

What i want to do is to use the same database generated automatically by the Identity 2 authentication, for my own POCO entity objects. And know where is it created.

Thanks!


Solution

  • The easiest way to combine them is to have your application context inherit from the identity context and set the connection string there:

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        static ApplicationDbContext()
        {
            // Set the database intializer which is run once during application start
            // Use an initializer that will create the database, or create it in SSMS first
            Database.SetInitializer(new CreateDatabaseIfNotExists<ApplicationDbContext>());
        }
    
        // Your identity and application tables will be created in the database this connect string points to
        public ApplicationDbContext()
            : base("DefaultConnection", false)
        {    }
    
        // define your POCOs here
        public virtual DbSet<Foo1> Foo1s { get; set; }
        public virtual DbSet<Foo2> Foo2s { get; set; }
        .. etc
    
    }