【发布时间】: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