我需要将 Asp.Net 最新 MVC 版本与 现有 数据库集成,该数据库具有附加列 String Address 以表 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,它具有属性 Address.
I need to create an instance ApplicationUser which has property Address.
知道怎么做吗?
推荐答案一个对我有用的可能解决方案,基本上我能够将 Asp.Net Identity User Profiles 与现有数据库集成.
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-[timestamp] 或类似的东西.)
- 使用 SQL Server Management Studio 编写数据库表脚本(为 mdc 附加数据库).
或者使用类似 identity.codeplex/
与您现有的数据库集成:
Integrating with your existing db:
- 将脚本表插入到 SQL Server Management Studio 中的现有数据库中.
- 自定义并添加与 ApplicationUser 的关系(如有必要).
- 创建新的 Web 项目 > MVC > DB First Project > 使用 EF 导入 DB ... .
- 在 IdentityModels.cs 中,更改 ApplicationDbContext :base("DefaltConnection") 以使用您项目的 DbContext.
现在您的数据库中有 Asp.Identity 表,应用程序中有 ER 模型.
Now you have the Asp.Identity Tables in your db with ER model in your application.
Asp.Identity Profile 添加新属性:
Asp.Identity Profile Adding new properties:
- 启用实体框架代码优先数据库迁移,只需在 VS 中进入工具包管理器控制台"下,
执行命令Enable-Migrations";启用数据库迁移后,我们可以继续为 UserProfile 添加新属性
- 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 Identity Database 第一种方法
发布评论