0

So I have a project with my custom, local DB (created by model-first approach). I need to create there identification tables (like asp_users, asp_roles, I don't remember how exactly they are called, but I hope you got the idea). As far as I know, they should be created on first registration, however it doesn't happen:

enter image description here

I can't see those tables in SQL Management Studio either. My connection string (created automatically with DB):

<!--<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-WebApplication1-20170504122316.mdf;Initial Catalog=aspnet-WebApplication1-20170504122316;Integrated Security=True" providerName="System.Data.SqlClient" />-->

<add name="DefaultConnection" connectionString="metadata=res://*/DBModel.csdl|res://*/DBModel.ssdl|res://*/DBModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=VADIM-PC;initial catalog=PIT_3_Project_DB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

The first connection string is truly default, the second is mine.

The fun part is - authentication works fine, I can register and login, and I don't see where my code cane use with the default local DB.

UPD: Sorry, I haven't changed this row:

    public ApplicationDbContext()
    : base("DBModelContainer", throwIfV1Schema: false)
    {
    }

I have changed DBModelContainer to DefaultConnection and now I am getting this error (when I am trying to register)

The entity type ApplicationUser is not part of the model for the current context.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

1 Answer 1

2

You should create these tables with code first migrations.

First step: Uncomment first default connection string,rename it for example DefaultAuthConnection and modify it like this (for local DB).

<add name="DefaultAuthConnection" connectionString="data source=.;initial catalog=PIT_3_Project_DB;persist security info=True;" providerName="System.Data.SqlClient" />

<add name="DefaultConnection" connectionString="metadata=res://*/DBModel.csdl|res://*/DBModel.ssdl|res://*/DBModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=VADIM-PC;initial catalog=PIT_3_Project_DB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

Second Step: Open your IdentityModels.cs and modify ApplicationDBContext like this:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("DefaultAuthConnection", throwIfV1Schema: false)
        {
        }
    }

Third Step: Open Package Manager Console (View->Other Windows->Package Manager Console) and write follow steps:

  1. Enable-Migrations -ContextTypeName WebApplication1.ApplicationDbContext
  2. Add-Migration Init
  3. Update-Database

From now on, you can see authentication tables in your local database.

Sign up to request clarification or add additional context in comments.

4 Comments

Got this error The context type 'aspnet-WebApplication1.ApplicationDbContext' was not found in the assembly 'WebApplication1'. on the 3rd step, point 1.
It it your application name. I wrote it for an example. So if your application name is WebApplication1, you can use 'Enable-Migrations -ContextTypeName WebApplication1.ApplicationDbContext'.
My application is 'WebApplication1' , however i still keep getting this error.
Try 'Enable-Migrations' alone without '-ContextTypeName ...'. It will give you two possible options for enabling migrations. After that we will select one which is related with DefaultAuthConnection.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.