使用Asp.Net身份数据库第一种方法 [英] Using Asp.Net Identity DataBase first approach
问题描述
我要Asp.Net MVC的最新版本具有的现有的有一个附加列字符串地址
来表数据库<$ C $整合C> dbo.AspNetUsers
I need to integrate Asp.Net latest MVC version with an existing database which has an additional column String Address
to table dbo.AspNetUsers
我需要创建一个实例 ApplicationUser
有物业地址。
I need to create an instance ApplicationUser
which has property Address.
任何想法,该怎么办呢?
Any idea how to do it?
推荐答案
一个可能的解决方案,这对我的作品,基本上我能Asp.Net身份的用户配置文件与现有数据库集成。
A possible solution which works for me, basically I am able to integrate Asp.Net Identity User Profiles with an existing Database.
获取Asp.Identity表:
Getting the Asp.Identity Tables:
- 与认证个人用户的帐号创建MVC项目
- 打开Web.config中的DefaultConnection中列出的数据库。它将被称为(aspnet- [时间戳]或类似的东西。)
- 脚本的数据库使用SQL Server Management Studio中(附加数据库MDC)表
或者使用类似 HTTP://identity.$c$cplex.com/
与您现有的数据库集成:
Integrating with your existing db:
- 将脚本表到SQL Server Management Studio中现有的数据库。
- 自定义,并添加关系,ApplicationUser(如有必要)。
- 创建新的Web项目> MVC> DB一是项目>导入数据库与EF ...。
- 在IdentityModels.cs改变ApplicationDbContext:基地(DefaltConnection),用你的项目的DbContext 。
现在你有你的分贝Asp.Identity表与ER模型在应用程序中。
Now you have the Asp.Identity Tables in your db with ER model in your application.
Asp.Identity档案添加新的属性:
Asp.Identity Profile Adding new properties:
- 启用实体框架code首先数据库迁移,只需在VS去下工具程序包管理器控制台,
-
执行命令启用的迁移;一旦我们启用了数据库迁移,我们可以继续为我们的用户配置添加新的属性
- 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
要添加新的属性修改IdentityModels.cs文件,例如:
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.
添加迁移YouMigrationName
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.
这篇关于使用Asp.Net身份数据库第一种方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!