简体   繁体   中英

ASP.NET MVC identification tables creation

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:

在此处输入图片说明

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM