1

I have 2 projects in my solution

  1. Web UI
  2. Web API

I am using Web API project for authentication (ASsp.Net Identity Framework). Below is the web.config setting for the connection string in Web.API project

<connectionStrings>
    <add name="AuthContext" connectionString="Data Source=./SQLEXPRESS;Initial Catalog=TestDB;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />
  </connectionStrings>

My DbContext Class

public class AuthContext : IdentityDbContext<IdentityUser>
    {
        public AuthContext()
            : base("AuthContext",throwIfV1Schema:false)
        {

        }

        public static  AuthContext Create()
        {
            return new AuthContext();
        }

        public DbSet<Client> Clients { get; set; }
        public DbSet<RefreshToken> RefreshTokens { get; set; }
    }

TestDB is an existing database with few tables. This is what I did in Web API project in the Package Manager Console

  1. Enable-Migrations
  2. Add-Migration InitialCreate
  3. Update-Database

It always create a new database called AuthContext instead of adding the necessary tables in TestDB.

I set the Web UI project as the Startup Project in my solution. How do I get update-database to create tables in TestDb instead of creating of new database.

-Alan-

Alan B
  • 2,219
  • 4
  • 32
  • 62

1 Answers1

1

Let's assume you have a connection string like this:

<connectionStrings>
    <add name="TestDB" connectionString="Data Source=./SQLEXPRESS;
AttachDbFilename=|DataDirectory|\TestDB.mdf;Initial Catalog=TestDB;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />
  </connectionStrings>

by default In your Models folder there is a file called IdentityModels.cs in that file you can change the connection string name in the constructor, but since you have a custom class, I think the concept is the same:

public class AuthContext : IdentityDbContext<ApplicationUser>
{
    public AuthContext()
        : base("TestDB",throwIfV1Schema:false)
    {

    }

    public static  AuthContext Create()
    {
        return new AuthContext();
    }

    public DbSet<Client> Clients { get; set; }
    public DbSet<RefreshToken> RefreshTokens { get; set; }
}
Hamid Mosalla
  • 3,279
  • 2
  • 28
  • 51
  • The connection string pointing to my TestDb but it still creating a new DB – Alan B Aug 25 '15 at 13:24
  • @AlanB Can you update your question with your `TestDb` context class and and `IdentityModels` also your `TestDb` connection string? as far as I know there is nothing really to it other than that, also take a look at [this](http://stackoverflow.com/questions/18297052/asp-net-identity-how-to-set-target-db) see if it solves your problem. – Hamid Mosalla Aug 25 '15 at 13:40
  • @AlanB I've updated the answer based on your update, I'm not sure the name should be the same but this class is responsible for creating new data store when you create your first username, if it didn't work fall back to the default name and what the template created for you. – Hamid Mosalla Aug 25 '15 at 14:30