ASP.NET Identity 2.0 DB First Approach: Adding new Columns db-first entity-framework-6


I am trying to use ASP.NET Identity 2.0 with existing database. I have created an MVC project (which uses Individual Account Authentication), then I registered for a use in order to create the DB.


  1. I created scripts for the necessary tables and added them to my own DB
  2. I added ADO.NET Entity Data Model (database first) which include my tables plus identity tables.
  3. I ran the application and registered for a user, everything is going fine.

Now, I need to add a relation to AspNetUser table.

  1. I added the Column LocationId with the relationship in DB.
  2. I Added the following to the Application User Class:

    public virtual Region Region { get; set; }
  3. Then, I updated my Model and run the application, when I tried to register for new user, I got the following error:

    AspNet UserLogin: EntityType: EntitySet 'AspNetUserLogins' is based on type 'AspNet UserLogin' that has no keys defined.

How is it possible to continue using DB First Approach in this scenario?

7/2/2016 9:44:12 PM

Accepted Answer

Identity uses Code First approach for making Identity System make customization as more as possible and you are using DB first approach for your common data access. So there are 2 contexts, one is for your data and other is for you Identity. You need to write Identity classes and make a code first migration of Identity context class by typing in the Package Manager Console as:

`Enable-Migrations -ContextNameType [Your_Identity_Context]

This will enable code first migrations just for your Identity context type. If you want to add region property in your user table, then in 'ApplicationUser' (or any class derived from IdentityUser) add the required region property and then apply the migrations to update the user table in the database.

Generating SQL script and applying to the database is not a good approach.

7/2/2016 11:18:52 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow