rwkiii
rwkiii

Reputation: 5846

EF Data First with ASP.NET Identity and MVC 5

I've created an ASP.NET MVC 5 application using Individual User Accounts for authentication and SQL Server 2012 Express.

The template seems to have worked. I can create accounts, manage my user account, etc. Now I would like to use EF to create additional tables for my app to use, but I'm doing something wrong since conflicting classes (like ApplicationDbContext{}) are being created.

Basically, I have my default AspNet database table and I want to see it in the EF Designer. There are some fields I would like to add to AspNetUsers table.

The way I am doing this is to "Add Item" and then I choose "EF Designer from database".

What is the method for including the default AspNet* tables in EF Designer without generating conflicting classes created by the initial ASP.NET Template? I'm using VS 2015 Community.

Upvotes: 0

Views: 133

Answers (1)

David Lee
David Lee

Reputation: 2100

Since you are using Identity I would not use the "Add Item" option to make any changes to your database.

To add new properties to the ApplicationUser model locate the file IdentityModels.cs. You should see something similar to below.

public class ApplicationUser: IdentityUser
{
    // add new properties here
    public DateTime DateOfBirth { get; set; }
}

This file is also where you will put your new tables/models.

public class ApplicationDbContext: IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext() 
        : base("DefaultConnection", throwIfVlSchema: false)
    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
    // Add new tables here
    public DbSet<NewModel> NewModels { get; set; }
}

After you have made any changes you will need to use EF Data Migrations to commit those changes to your database.

To install Migrations use the Package Manager Console and type Enable-Migrations

To add a migration type add-migration [enter a description]

To commit to the database type update-database

Upvotes: 3

Related Questions