jamheadart
jamheadart

Reputation: 5313

Asp.Net identity and CodeFirst migrations

I installed nuget packages for asp.net identity and followed this MS tutorial which, by creating a new user, creates all of the tables e.g. AspNetRoles, AspNetUsers in my database.

The problem is, I'm trying to use CodeFirst migrations for DB source control and my Visual Studio side has none of the models for these tables. I don't want the "Create user" method to automatically create the tables, I need to get my models in Visual Studio and then push it using a migration.

Currently I have all the [AspNet] tables in my database and no reflection of this in my migrations. I can delete the tables but need to know how to populate the code first!

I'll also probably need to customise the models later, but that's another issue.

Upvotes: 0

Views: 46

Answers (2)

Md. Asaduzzaman
Md. Asaduzzaman

Reputation: 135

You can extent your asp.net identity as bellow

 public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public DbSet<Employee> Employees { get; set; }
        public DbSet<Attendance> Attendances { get; set; }
        public DbSet<PunchRecord> PunchRecords { get; set; }
    }

Upvotes: 1

jamheadart
jamheadart

Reputation: 5313

Took me two hours to find I simply needed to add a model that inherits IdentityUser and then add a migration. I've called the model "ApplicationUser" - based on a tutorial I found on how to extend AspNetRoles and an answer somewhere in stackoverflow, it seems this name is possibly automatic or scaffolded somewhere... but I just chucked it manually in my Models folder:

public class ApplicationUser : IdentityUser
{
}

Also, I had to tinker with my application's context to inherit from IdentityDbContext - this way I don't need two separate connection strings and my app context is now all inclusive.

Upvotes: 0

Related Questions