EF Code first Ошибка при попытке добавить связь между таблицей Asp.net Identity ApplicationUsers и другой таблицей - PullRequest
0 голосов
/ 06 ноября 2018

Я пытаюсь добавить связь между таблицей 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 голосов
/ 07 ноября 2018

попробуйте изменить это

public UserType UserType { get; set; }

к этому

public virtual UserType UserType { get; set; }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...