How to create ASP.net identity tables in an already created database using code first?

后端 未结 4 1495
醉话见心
醉话见心 2020-12-13 11:01

My application has been in development for about a month. I now decided to use ASP.NET Identity. I already have the view models for identity but need to create the tables. I

4条回答
  •  既然无缘
    2020-12-13 11:28

    Consider Migrations

    If applicable, you need to consider building a migration, which will allow you to generate (and potentially execute) the necessary scripts to create the appropriate tables or changes within your database.

    By default, you should have some type of ApplicationDbContext class that looks like the following which will be used to define your "security-related" database:

    public class ApplicationDbContext : IdentityDbContext
    {
        public ApplicationDbContext()
            : base("DefaultConnection", false)
        {
        }
    
        // Other code omitted for brevity
    }
    

    You'll then just need to run the Enable-Migrations command in Package Manager Console:

    Enable-Migrations
    

    This should generate a Migrations folder within your application that contains various configuration files that control how migrations are preformed as well as an InitialCreate migration. This may only be present if you previously had some Code-First related code within your application, if not, don't worry about it. You can then try running the Update-Database command, which should execute any migrations (including an initial one) against your database:

    Update-Database
    

    Once your database has been updated, you can continue to make changes to your model and simply create and execute new migrations through the Add-Migration command and the previous Update-Database command:

    Add-Migration "AddedAnotherPropertyToFoo"
    Update-Database
    

提交回复
热议问题