【发布时间】:2019-10-08 23:29:10
【问题描述】:
我想使用 ASP.Net-Identity 进行用户管理。为此,我想用一些属性扩展IdentityUser 类。
public class AppUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<AppUser> manager)
{
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
return userIdentity;
}
public int Settings_ID { get; set; }
public string Position { get; set; }
public string CompanyName { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
[...]
现在,在我的data context 中,我只想为这个用户模型创建一个表:
public class AntContext : IdentityDbContext<AppUser>
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Database.SetInitializer<AntContext>(null);
modelBuilder.Entity<AppUser>().ToTable("AppUsers");
base.OnModelCreating(modelBuilder);
}
public override IDbSet<AppUser> Users { get; set; }
[...]
但是,当我尝试update-database 时,我收到错误:
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_dbo.AppUsers_dbo.AspNetUsers_Id". The conflict occurred in database "C:\USERS\NEUMA\SOURCE\REPOS\MYANTON\MYANTON\APP_DATA\ASPNETDB.MDF", table "dbo.AspNetUsers", column 'Id'.
我知道AspNetUsers 是来自IdentityUser 的一个表,但是如何只创建一个表才能不发生冲突?
如何正确扩展 IdentityUserclass 并在我的数据上下文中使用它?
【问题讨论】:
标签: c# asp.net-mvc asp.net-identity