Using Asp.Net Identity DataBase first approach

后端 未结 4 773
广开言路
广开言路 2020-11-27 11:09

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

相关标签:
4条回答
  • 2020-11-27 11:20

    public class MyUser : IdentityUser { public virtual MyUserInfo MyUserInfo { get; set; } }

    public class MyUserInfo{ 
        public int Id { get; set; } 
        public string FirstName { get; set; } 
        public string LastName { get; set; } 
    } 
    public class MyDbContext : IdentityDbContext<MyUserInfo> //Edited to MyUserInfo
    { 
        public MyDbContext() 
            : base("DefaultConnection") 
        { 
        } 
        public System.Data.Entity.DbSet<MyUserInfo> MyUserInfo { get; set; } 
     } 
    

    Getting Profile information

    When the User Logs in, you can display the profile information by doing the following Get the current logged in UserId, so you can look the user up in ASP.NET Identity system

    var currentUserId = User.Identity.GetUserId(); 
    

    Instantiate the UserManager in ASP.Identity system so you can look up the user in the system

    var manager = new UserManager<MyUser>(new UserStore<MyUser>(new MyDbContext())); 
    

    Get the User object

    var currentUser = manager.FindById(User.Identity.GetUserId()); 
    

    Get the profile information about the user

    currentUser.MyUserInfo.FirstName 
    
    0 讨论(0)
  • 2020-11-27 11:21

    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.

    0 讨论(0)
  • 2020-11-27 11:27

    Take a look at these projects on GitHub:

    • https://github.com/kriasoft/AspNet.Identity - Identity Database Project + VS Template
    • https://github.com/kriasoft/AspNet-Server-Template - Sample / reference project

    Which includes:

    • SQL Database Project Template for ASP.NET Identity 2.0
    • Entity Framework Database-First Provider(s)
    • Source Code and Samples

    0 讨论(0)
  • 2020-11-27 11:42

    I have been successful in integrating an existing database via Database First with Identity Framework 2.0. I have written a blog post on it here which uses ASP.NET MVC 5, Identity Framework 2.0, and the SPA template from Visual Studio 2013 Update 2 RTM.

    I hope this helps anyone in their journey because it contains a few points not listed in one place which I had to figure out on my own.

    0 讨论(0)
提交回复
热议问题