我需要将最新的Asp.Net MVC版本与一个已有的数据库集成,该数据库的表dbo.AspNetUsers
中有一个额外的列String Address
。
我需要创建一个名为ApplicationUser
的实例,其中包含属性Address。
你有任何想法如何做到这一点吗?
我需要将最新的Asp.Net MVC版本与一个已有的数据库集成,该数据库的表dbo.AspNetUsers
中有一个额外的列String Address
。
我需要创建一个名为ApplicationUser
的实例,其中包含属性Address。
你有任何想法如何做到这一点吗?
一个适用于我的可能解决方案,基本上我能够将Asp.Net Identity用户档案与现有数据库集成。
获取Asp.Identity表:
或者可以使用像http://identity.codeplex.com/这样的工具。
与现有数据库集成:
现在,您的数据库中具有Asp.Identity表和ER模型。
Asp.Identity添加新属性:
执行命令“Enable-Migrations”; 一旦我们启用了数据库迁移,我们就可以继续为UserProfile添加新属性
要添加新属性,请修改IdentityModels.cs文件,例如:
public class ApplicationUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string EmailID { get; set; }
}
添加新迁移
在添加属性后,打开程序包管理器控制台并执行以下命令:
Add-Migration "YouMigrationName"
此命令将生成一个数据库脚本文件,现在执行以下命令来运行此脚本文件以针对数据库运行。
Update-Database
现在,所有新属性都会成为同一个数据库表中的表字段。其中包括:
dbContext
。 - Zapnologica最近我也遇到了同样的问题。我使用了DBFirst方法创建了一个应用程序,现在需要添加Identity。以下是我的解决方案:
1. Microsoft.EntityFrameworkCore
2. Microsoft.EntityFrameworkCore.Design
3. Microsoft.EntityFrameworkCore.SqlServer
4. Microsoft.AspNetCore.Identity
5. Microsoft.AspNetCore.Identity.EntityFrameworkCore
6. Microsoft.AspNetCore.Aututhentication.JwtBearer
public partial class BookStoresDBContext : IdentityDbContext
protected override void OnModelCreating(ModelBuilder modelBuilder) {
base.OnModelCreating(modelBuilder);
}
如果这是一个已创建的项目,那么StringConnection应该已经存在,如果不存在,请添加它。
在Startup.cs文件中,在ConfigureServices方法中配置Identity服务。
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<BookStoresDBContext>(options => options.UseSqlServer(Configuration.GetConnectionString("BookStoreDB")));
services.AddIdentity<IdentityUser, IdentityRole>(options =>
{
options.Password.RequireDigit = true;
options.Password.RequiredLength = 5;
}).AddEntityFrameworkStores<BookStoresDBContext>()
.AddDefaultTokenProviders();
}
You can configure the Authetication service too
services.AddAuthentication(auth =>
{
auth.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
auth.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options => {
options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
RequireExpirationTime = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("Your key to encrypt"))
};
});
Then run the migration from the Package Manager Console
Add-Migration InitDb
在迁移文件中,删除所有已经存在于数据库中的表的 migrationBuilder.CreateTable 代码块。
从程序包管理控制台更新数据库。
Update-Database
希望这对您有所帮助。
Scaffold-DbContext
/dotnet ef dbcontext scaffold
遇到现有的'AspNetxxx'表进行操作。你所生成的模型已经包含了 Identity。试试这个练习: 使用模板构建一个基于代码的.NET 6 MVC 应用程序,带有独立账户。进行初始迁移以创建数据库。运行, 注册, 登录/登出。然后删除解决方案中的迁移文件夹和数据库中的迁移表。应用仍能顺利运行。(它还是代码优先吗?)看一下ApplicationDBContext.cs
文件。现在开始考虑你的自定义需求。 - Ian W不要忘记添加迁移并更新数据库,否则会为身份验证抛出依赖注入异常。
公共类 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; }
}
获取个人资料信息
当用户登录时,您可以通过以下方式显示个人资料信息:
获取当前已登录的UserId
,以便您可以在ASP.NET身份验证系统中查找该用户
var currentUserId = User.Identity.GetUserId();
UserManager
,以便您可以在系统中查找用户。var manager = new UserManager<MyUser>(new UserStore<MyUser>(new MyDbContext()));
var currentUser = manager.FindById(User.Identity.GetUserId());
currentUser.MyUserInfo.FirstName