How to create ASP.Net Identity tables inside existing database?

后端 未结 4 384
一向
一向 2020-12-13 20:03

I am building my first MVC 5 / Entity Framework application. I used the database first method to pull in my data from an existing SQL server. The existing SQL database rec

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-13 20:23

    With DB First, changing the connection string is not going to cause Identity 2.0 to create tables in the DB.

    CORRECTION: I originally had my Identity tables in the DB under an Identity schema and when I registered new users using the below, new tables were written into my DB under the dbo schema.

    You have to create a dummy project first using Code First, build and run the project, go onto the now running app in your web browser, register a user with a dummy email and password, this will cause Entity FrameWork Code First(?) to create all the Identity 2.0 DB tables in your dummy database. You'll then want to export the dummy tables to SQL script and import them into your existing DB that you want to use them in. There should be 5 tables: AspNetUserRoles, AspNetRoles, AspNetUsers, AspNetUserClaims, and AspNetUserLogins.

    I have a ADO.Net Entity Model(.edmx file) for my main DB models and created another .edmx for the Identity Models(I named: IdentityDbEntities). That's when you should change the connection string from "DefaultConnection":

        public class ApplicationDbContext : IdentityDbContext
        {
            public ApplicationDbContext()
                : base("IdentityDbEntitiesString", throwIfV1Schema: false)
            {
            }
    

    VERY IMPORTANT: In your Web.config file, you must add an extra connection string that you'll be using above. Looks like (I'm using a SQL Server Dev environment, so your connection strings could change):

      
        
                 
    

    Anything inside #'s like #DbName# will be custom to you.

提交回复
热议问题