ED 代码第一个一对多关系问题 - ASP 身份
ED Code First One to Many Relationship Issue - ASP Identity
我正在为 ASP 身份配置数据库,但遇到了障碍。
我想在两个表之间创建 One to Many
关系,它们是 ApplicationUser
和 OrganisationUnit
。
一个OrganisationUnit可以有多个ApplicationUsers
,其中一个ApplicationUser
只属于一个OrganisationUnit
当我添加迁移并更新数据库时,我在执行期间遇到错误:-
System.Data.SqlClient.SqlException: The INSERT statement conflicted
with the FOREIGN KEY constraint
"FK_dbo.AspNetUsers_dbo.OrganisationUnits_OrganisationUnitRefId". The
conflict occurred in database "DefaultConnection", table
"dbo.OrganisationUnits", column 'OrganisationUnitId'.
这是我要创建的表:-
public class ApplicationUser : IdentityUser
{
[Required]
[MaxLength(100)]
public string Forename { get; set; }
[Required]
[MaxLength(100)]
public string Surname { get; set; }
[Required]
public DateTime DateCreated { get; set; }
public Guid OrganisationUnitId { get; set; }
[ForeignKey("OrganisationUnitId")]
public virtual OrganisationUnit OrganisationUnit { get; set; }
}
public class OrganisationUnit
{
public OrganisationUnit()
{
ApplicationUsers = new List<ApplicationUser>();
}
public Guid Id { get; set; }
[Required]
[MaxLength(100)]
public string Name { get; set; }
[Required]
[MaxLength(100)]
public string Telephone { get; set; }
public virtual ICollection<ApplicationUser> ApplicationUsers { get; set; }
}
在我的 Configuration.cs 种子方法中有以下代码:-
protected override void Seed(ApplicationDbContext context)
{
// This method will be called after migrating to the latest version.
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
var ou = new OrganisationUnit()
{
Id = Guid.NewGuid(),
Name = "Group1",
Telephone = "1234567890",
};
var user = new ApplicationUser()
{
UserName = "SuperPowerUser",
Email = "derek@rdsadfasdf.com",
EmailConfirmed = true,
Forename = "Derek",
Surname = "Rivers",
DateCreated = DateTime.Now.AddYears(-3),
OrganisationUnitId = ou.Id
};
userManager.Create(user, "MySuperP@ssword!");
}
}
你应该试试 :
var user = new ApplicationUser()
{
UserName = "SuperPowerUser",
Email = "derek@rdsadfasdf.com",
EmailConfirmed = true,
Forename = "Derek",
Surname = "Rivers",
DateCreated = DateTime.Now.AddYears(-3),
OrganisationUnit = ou
};
根据您的语法(仅提供 FK),您假设该组织存在。
我正在为 ASP 身份配置数据库,但遇到了障碍。
我想在两个表之间创建 One to Many
关系,它们是 ApplicationUser
和 OrganisationUnit
。
一个OrganisationUnit可以有多个ApplicationUsers
,其中一个ApplicationUser
只属于一个OrganisationUnit
当我添加迁移并更新数据库时,我在执行期间遇到错误:-
System.Data.SqlClient.SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.AspNetUsers_dbo.OrganisationUnits_OrganisationUnitRefId". The conflict occurred in database "DefaultConnection", table "dbo.OrganisationUnits", column 'OrganisationUnitId'.
这是我要创建的表:-
public class ApplicationUser : IdentityUser
{
[Required]
[MaxLength(100)]
public string Forename { get; set; }
[Required]
[MaxLength(100)]
public string Surname { get; set; }
[Required]
public DateTime DateCreated { get; set; }
public Guid OrganisationUnitId { get; set; }
[ForeignKey("OrganisationUnitId")]
public virtual OrganisationUnit OrganisationUnit { get; set; }
}
public class OrganisationUnit
{
public OrganisationUnit()
{
ApplicationUsers = new List<ApplicationUser>();
}
public Guid Id { get; set; }
[Required]
[MaxLength(100)]
public string Name { get; set; }
[Required]
[MaxLength(100)]
public string Telephone { get; set; }
public virtual ICollection<ApplicationUser> ApplicationUsers { get; set; }
}
在我的 Configuration.cs 种子方法中有以下代码:-
protected override void Seed(ApplicationDbContext context)
{
// This method will be called after migrating to the latest version.
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
var ou = new OrganisationUnit()
{
Id = Guid.NewGuid(),
Name = "Group1",
Telephone = "1234567890",
};
var user = new ApplicationUser()
{
UserName = "SuperPowerUser",
Email = "derek@rdsadfasdf.com",
EmailConfirmed = true,
Forename = "Derek",
Surname = "Rivers",
DateCreated = DateTime.Now.AddYears(-3),
OrganisationUnitId = ou.Id
};
userManager.Create(user, "MySuperP@ssword!");
}
}
你应该试试 :
var user = new ApplicationUser()
{
UserName = "SuperPowerUser",
Email = "derek@rdsadfasdf.com",
EmailConfirmed = true,
Forename = "Derek",
Surname = "Rivers",
DateCreated = DateTime.Now.AddYears(-3),
OrganisationUnit = ou
};
根据您的语法(仅提供 FK),您假设该组织存在。