扩展IdentityUser,外键约束冲突

Extend IdentityUser, foreign key constraint conflict

我想使用ASP.Net-Identity 进行用户管理。为此,我想用一些属性扩展 IdentityUser class。

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 中,我只想为这个用户模型创建一个 table:

 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 的 table,但是如何只创建一个 table 才不会发生冲突?

如何正确扩展 IdentityUserclass 并在我的数据上下文中使用它?

解决方案是不要混合上下文。保持关注点分离。为身份使用迁移脚本并为业务上下文(您的数据上下文)创建一个新脚本。

将用户 table 添加到引用 IdentityContext 中的名称或子项的业务上下文。请阅读我的回答 对于一个非常相似的问题。

此外,无需扩展 ApplicationUser table。您可以使用 AspNetUserClaims 添加此类信息。使用自定义类型:

new Claim("http://www.myapp.com/Firstname", "Firstname");

或现有的 WIF ClaimType:

new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname", "Lastname");

至于 CompanyName,这实际上可能是业务上下文的一部分。