尝试在Asp.net Identity ApplicationUsers表和另一个表之间添加关系时,EF代码第一个错误

时间:2018-11-06 20:13:28

标签: asp.net-mvc

我试图在ApplicationUsers表和表之间添加关系,并添加名称UserType,并且在添加迁移后出现此错误:

ALTER TABLE语句与FOREIGN KEY约束“ FK_dbo.AspNetUsers_dbo.UserTypes_UserTypeId”冲突。在数据库“ TestDB”的表“ dbo.UserTypes”的列“ Id”中发生了冲突。

我的代码:

 public class UserType
{
    public UserType()
    {
        Users = new HashSet<ApplicationUser>();
    }
    public int Id { get; set; }

    [Required]
    [StringLength(255)]
    [Display(Name="Acount Type")]
    public string Name { get; set; }

    public ICollection<ApplicationUser> Users { get; set; }
}

public class ApplicationUser : IdentityUser
{
    public int UserTypeId { get; set; }

    public UserType UserType { get; set; }

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public DbSet<UserType> UserTypes { get; set; }

    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

1 个答案:

答案 0 :(得分:0)

尝试更改此内容

public UserType UserType { get; set; }

对此

public virtual UserType UserType { get; set; }