ASP.NET Identity with EF Database First MVC5 ASP.NET Identity with EF Database First MVC5 asp.net asp.net

ASP.NET Identity with EF Database First MVC5


It should be possible to use the identity system with POCO and Database First, but you'll have to make a couple of tweaks:

  1. Update the .tt-file for POCO generation to make the entity classes partial. That will make it possible for you to supply additional implementation in a separate file.
  2. Make a partial implementation of the User class in another file

 

partial User : IUser{}

That will make the User class implement the right interface, without touching the actual generated files (editing generated files is always a bad idea).


My steps are very similar but I wanted to share.

1) Create a new MVC5 project

2) Create a new Model.edmx. Even if it's a new database and has no tables.

3) Edit web.config and replace this generated connectionstring:

<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-SSFInventory-20140521115734.mdf;Initial Catalog=aspnet-SSFInventory-20140521115734;Integrated Security=True" providerName="System.Data.SqlClient" />

with this connectionstring:

<add name="DefaultConnection" connectionString="Data Source=.\SQLExpress;database=SSFInventory;integrated security=true;" providerName="System.Data.SqlClient" />

Afterwards, build and run the application. Register a user and then the tables will be created.


EDIT: ASP.NET Identity with EF Database First for MVC5 CodePlex Project Template.


I wanted to use an existing database and create relationships with ApplicationUser. This is how I did it using SQL Server but the same idea would probably work with any DB.

  1. Create an MVC Project
  2. Open the DB listed under the DefaultConnection in Web.config. It will be called (aspnet-[timestamp] or something like that.)
  3. Script the database tables.
  4. Insert the scripted tables into existing database in SQL Server Management Studio.
  5. Customize and add relationships to ApplicationUser (if necessary).
  6. Create new Web Project > MVC > DB First Project > Import DB with EF ... Excluding the Identity Classes you inserted.
  7. In IdentityModels.cs change the ApplicationDbContext :base("DefaltConnection") to use your project's DbContext.

Edit: Asp.Net Identity Class Diagramenter image description here