繁体   English   中英

扩展IdentityUser,外键成本冲突

[英]Extend IdentityUser, foreign key costraint conflict

我想使用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'.

我知道AspNetUsersIdentityUser一个表,但是如何仅创建一个表,以免发生冲突?

如何正确扩展IdentityUser类并在数据上下文中使用它?

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

将用户表添加到引用IdentityContext中名称或子名称的业务上下文中。 在这里阅读我的答案,以解决类似的问题。

同样,也不需要扩展ApplicationUser表。 您可以使用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,这实际上可能是业务环境的一部分。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM