【问题标题】:EF Code first Error when try to add relation between Asp.net Identity ApplicationUsers table and Another table尝试在 Asp.net Identity ApplicationUsers 表和另一个表之间添加关系时,EF 代码首先出错
【发布时间】:2018-11-06 20:13:27
【问题描述】:

我正在尝试添加 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();
    }
}

【问题讨论】:

    标签: asp.net-mvc


    【解决方案1】:

    尝试改变这个

    public UserType UserType { get; set; }
    

    到这里

    public virtual UserType UserType { get; set; }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-17
      • 1970-01-01
      • 2015-10-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多