Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 was thinking and I am not exactly sure why I do not have the tables already if I have the view models?? I have drop create on in the initializer with my own custom context, I just need to know how to get EF to build the included Identity tables for users and roles? I looked around and none of the posted answers seem to be what I need?

like image 221
LeRainman Avatar asked Apr 17 '17 20:04

LeRainman


People also ask

How will you add a new table to an existing database code first in .NET core?

Pretty simple actually. Create or reverse engineer the other schema POCOs. Add any fluent config needed (or get it from reverse engineered class)...public virtual DbSet<OtherSchemaTable> OtherSchemaTable {get; set; }' ... ...Create a new migration to replace your snapshot, but not update the database:...


1 Answers

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<ApplicationUser>
{
    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
like image 99
Rion Williams Avatar answered Oct 30 '22 13:10

Rion Williams