【发布时间】:2017-02-18 21:06:01
【问题描述】:
我有一个 web 应用程序,有两个 ApplicationUser,Cursist 和 Lector:
public class ApplicationUser : IdentityUser
{
public string Sex { get; set; }
public string FName { get; set; }
public string LName { get; set; }
//email inherited by IdentityUser
....
}
public class Cursist: ApplicationUser
{
public Group Group { get; set; }
public int GroupId { get; set; }
....
}
public class Lector: ApplicationUser
{
private IEnumerable<Group> _supervising;
....
}
这是我第一次对 ApplicationUser 使用继承,这导致了一个不断的错误:
System.Data.SqlClient.SqlException:INSERT 语句与 FOREIGN KEY 约束“FK_AspNetUserClaims_AspNetUsers_UserId”冲突。冲突发生在数据库“cvodb”、表“dbo.AspNetUsers”、“Id”列中。 声明已终止。
错误的根源似乎来自这段代码sn-p:
public async Task InitializeData()
{
_dbContext.Database.EnsureDeleted();
if (_dbContext.Database.EnsureCreated())
{
await InitializeUsers();
_dbContext.SaveChanges();
}
}
private async Task InitializeUsers()
{
ApplicationUser user = new Cursist("Male", "fname", "lname", "b@b.b");
await _userManager.CreateAsync(user, "password");
await _userManager.AddClaimAsync(user, new Claim(ClaimTypes.Role, "Cursist"));
}
修改后编辑 我设法通过将用户名添加到 ApplicationUserId 来解决 UserId 冲突。这些类现在看起来像这样:
public class ApplicationUser : IdentityUser
{
public string Sex { get; set; }
public string FName { get; set; }
public string LName { get; set; }
//email inherited by IdentityUser
public ApplicationUser(string Sex, string fname, string lname, string email)
{
base.Email = email;
//username fixes foreign key conflict?
base.UserName = email;
Sex = sex;
FName = fname;
LName = lname;
}
}
public class Cursist: ApplicationUser
{
public Group Group { get; set; }
// can be null
public int? GroupId { get; set; }
}
//Lector class hasn't changed.
我在控制台中得到这个输出(忽略拼写差异):
ALTER TABLE [AspNetUsers] 添加约束 [FK_AspNetUsers_Groep_GroepId] 外键 ([GroepId]) 引用 [Groep] ([GroepId]) ON DELETE NO ACTION; Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommandBuilderFactory:Information: Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
ALTER TABLE [AspNetUsers] 添加约束 [FK_AspNetUsers_Groep_GroepId1] 外键 ([GroepId1]) 引用 [Groep] ([GroepId]) ON DELETE NO ACTION; Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommandBuilderFactory:Information: Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
ALTER TABLE [AspNetUsers] 添加约束 [FK_AspNetUsers_Groep_GroepId2] 外键 ([GroepId2]) 引用 [Groep] ([GroepId]) ON DELETE NO ACTION;
我不知道为什么 AspNetUsers 会创建 3 个 groupIds 实例?第一个实例和第二个实例不会产生错误,但最后一个会:
{Microsoft.EntityFrameworkCore.DbUpdateException:更新条目时出错。有关详细信息,请参阅内部异常。 ---> System.Data.SqlClient.SqlException:INSERT 语句与 FOREIGN KEY 约束“FK_AspNetUsers_Groep_GroepId2”冲突。冲突发生在数据库“cvodb”、表“dbo.Groep”、列“GroepId”中。 声明已终止。
我感觉它正在尝试链接 Group 对象,但是组可以是空值。我仍然不确定为什么会有 groupId、groupId1 和 groupId2。
我有一个名为 Group 的类和两个子类 ClosedGroup 和 OpenGroup。他们应该使用相同的密钥 GroupId,没有 Group 的实例。只有 ClosedGroups 和 OpenGroups。
【问题讨论】:
-
我假设用户被创建并返回给你,但例外是在最后一行
_userManager.AddClaimAsync。运行_userManager.CreateAsync后,运行调试器并检查user对象。是否填充了 ID 属性? -
@trailmax 确实创建并返回了用户。 Id 字段的值如下:“e8d2a128-08de-4520-95f4-75ede1a51c1c”,所以我想它已经成功填充了。
-
显然,当我向 ApplicationUser 添加用户名时,冲突已解决。然而,新的更奇怪的冲突出现了。更新问题。
标签: sql asp.net model-view-controller foreign-keys asp.net-identity