Я пытаюсь добавить связь между таблицей 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();
}
}