Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Asp.Net Identity DataBase first approach

I need to integrate Asp.Net latest MVC version with an existing database which has an additional column String Address to table dbo.AspNetUsers

I need to create an instance ApplicationUser which has property Address.

Any idea how to do it?

like image 725
GibboK Avatar asked Dec 18 '13 21:12

GibboK


People also ask

Can we use database first approach in ASP.NET Core?

EF Core Database-First Tutorial for . NET Core. Entity Framework Core supports Database-First approach via the Scaffold-DbContext command of Package Manager Console. This command scaffolds a DbContext and entity type classes for a specified database.

What is database first approach in ASP NET MVC?

The Database First Approach provides an alternative to the Code First and Model First approaches to the Entity Data Model. It creates model codes (classes, properties, DbContext etc.) from the database in the project and those classes become the link between the database and controller.

Why would you use ASP NET identity?

The ASP.NET Identity is a fresh look at what the membership system should be when you are building modern applications for the web, phone or tablet. ASP.NET Identity allows you to add customized login/logout functionality and customized profile features that make it easy to customize the data about the logged-in user.


1 Answers

A possible solution which works for me, basically I am able to integrate Asp.Net Identity User Profiles with an existing Database.

Getting the Asp.Identity Tables:

  • Create an MVC Project with Authentication Individual User Account
  • Open the DB listed under the DefaultConnection in Web.config. It will be called (aspnet-[timestamp] or something like that.)
  • Script the database tables using SQL Server Management Studio (attach database for mdc).

Alternatively use something like http://identity.codeplex.com/

Integrating with your existing db:

  • Insert the scripted tables into existing database in SQL Server Management Studio.
  • Customize and add relationships to ApplicationUser (if necessary).
  • Create new Web Project > MVC > DB First Project > Import DB with EF ... .
  • In IdentityModels.cs change the ApplicationDbContext :base("DefaltConnection") to use your project's DbContext.

Now you have the Asp.Identity Tables in your db with ER model in your application.

Asp.Identity Profile Adding new properties:

  • Enable Entity Framework Code First Database Migrations, just in VS go under Tools ‘Package Manager Console’,
  • Execute the command “Enable-Migrations”; Once we enabled the database migrations, we can go ahead and add new properties for our UserProfile

  • To Add new properties modify IdentityModels.cs file, example:


public class ApplicationUser : IdentityUser { public string FirstName { get; set; } public string LastName { get; set; } public string EmailID { get; set; } } 

Add New Migration

  • Once we added the properties, bring the Package Manager Console and execute the following command.

    Add-Migration “YouMigrationName”

This command will generate a database script file, now execute following command to run this script file against the database.

Update-Database 

Now, all the new properties will turn into table fields in the same database table.

I hope it can help others, if you have a better idea please let me know.

like image 176
GibboK Avatar answered Oct 12 '22 23:10

GibboK