INSERT 语句与具有用户 ID(ASP 身份)的 FOREIGN KEY 冲突
The INSERT statement conflicted with the FOREIGN KEY with user Id (ASP Identity)
我正在开发一项允许用户通过表单提交 post 的功能,它成功地从 AspNetUsers 获取了用户 ID。
我实现了一个单选按钮,让用户可以选择匿名提交。代码如下,
protected void AddPost(object sender, EventArgs e)
{
var User = System.Web.HttpContext.Current.User.Identity.GetUserId();
if (Convert.ToBoolean(Int32.Parse(inputAnonymous.SelectedValue)))
{
User = "anonymous";
}
Post newPost = new Post()
{
postTitle = inputTitle.Text,
postBody = inputBody.Text,
postDescription = inputDescription.Text,
postCategory = inputCategory.SelectedValue,
postAnonymous = Convert.ToBoolean(Int32.Parse(inputAnonymous.SelectedValue)),
Id = User
};
using (var _dbContext = new ApplicationDbContext())
{
_dbContext.Posts.Add(newPost);
_dbContext.SaveChanges();
}
}
当为用户匿名选择"No"选项时,post正常提交,并且post将id输入数据库等,但是当我点击"Yes" 当我跟踪调试器时,它说它识别它是匿名的,它应该将 userId 更改为字符串 "anonymous" 并将其作为数据库中的 Id 提交,但我保留得到这个阻止我的错误如下
SqlException:INSERT 语句与 FOREIGN KEY 约束冲突 "FK_dbo.Posts_dbo.AspNetUsers_Id"。冲突发生在数据库 "aspnet-WebEnterprise-20190201040107"、table "dbo.AspNetUsers"、列 'Id' 中。
声明已终止。
更新:
我已将这段代码添加到我的 configuration.cs
{
var passwordHash = new PasswordHasher();
string password = passwordHash.HashPassword("Anonymous@123");
context.Users.AddOrUpdate(u => u.UserName,
new ApplicationUser
{
UserName = "Anonymous@Anonymous.com",
PasswordHash = password,
PhoneNumber = "12345678911",
Email = "Anonymous@Anonymous.com"
});
context.Roles.AddOrUpdate(
new IdentityRole { Id = "a1f04ba5-5600-4a6e-be43-ae0d6360c0ab", Name = "Anonymous" }
);
}
但是当我在 nuget 数据包管理器上更新数据库时,出现以下错误
"An error occurred while updating the entries. See the inner exception for details."
没有 ID 为 "anonymous" 的用户,因此您会收到错误消息。 AspNetUsers 不包含 ID 为 "anonymous" 的任何用户。除了使用不存在的 Id 值外,此代码没有任何问题。
就这个问题而言,我的建议是创建一个匿名用户,获取他的 ID 并将该 ID 绑定到所有应该匿名的 post。所有匿名 post 都将具有相同的 ID,但这应该无关紧要,因为它们无论如何都是匿名的。查看 this and this link 了解更多信息,了解如何通过代码优先架构插入用户
我正在开发一项允许用户通过表单提交 post 的功能,它成功地从 AspNetUsers 获取了用户 ID。 我实现了一个单选按钮,让用户可以选择匿名提交。代码如下,
protected void AddPost(object sender, EventArgs e)
{
var User = System.Web.HttpContext.Current.User.Identity.GetUserId();
if (Convert.ToBoolean(Int32.Parse(inputAnonymous.SelectedValue)))
{
User = "anonymous";
}
Post newPost = new Post()
{
postTitle = inputTitle.Text,
postBody = inputBody.Text,
postDescription = inputDescription.Text,
postCategory = inputCategory.SelectedValue,
postAnonymous = Convert.ToBoolean(Int32.Parse(inputAnonymous.SelectedValue)),
Id = User
};
using (var _dbContext = new ApplicationDbContext())
{
_dbContext.Posts.Add(newPost);
_dbContext.SaveChanges();
}
}
当为用户匿名选择"No"选项时,post正常提交,并且post将id输入数据库等,但是当我点击"Yes" 当我跟踪调试器时,它说它识别它是匿名的,它应该将 userId 更改为字符串 "anonymous" 并将其作为数据库中的 Id 提交,但我保留得到这个阻止我的错误如下
SqlException:INSERT 语句与 FOREIGN KEY 约束冲突 "FK_dbo.Posts_dbo.AspNetUsers_Id"。冲突发生在数据库 "aspnet-WebEnterprise-20190201040107"、table "dbo.AspNetUsers"、列 'Id' 中。 声明已终止。
更新: 我已将这段代码添加到我的 configuration.cs
{
var passwordHash = new PasswordHasher();
string password = passwordHash.HashPassword("Anonymous@123");
context.Users.AddOrUpdate(u => u.UserName,
new ApplicationUser
{
UserName = "Anonymous@Anonymous.com",
PasswordHash = password,
PhoneNumber = "12345678911",
Email = "Anonymous@Anonymous.com"
});
context.Roles.AddOrUpdate(
new IdentityRole { Id = "a1f04ba5-5600-4a6e-be43-ae0d6360c0ab", Name = "Anonymous" }
);
}
但是当我在 nuget 数据包管理器上更新数据库时,出现以下错误
"An error occurred while updating the entries. See the inner exception for details."
没有 ID 为 "anonymous" 的用户,因此您会收到错误消息。 AspNetUsers 不包含 ID 为 "anonymous" 的任何用户。除了使用不存在的 Id 值外,此代码没有任何问题。
就这个问题而言,我的建议是创建一个匿名用户,获取他的 ID 并将该 ID 绑定到所有应该匿名的 post。所有匿名 post 都将具有相同的 ID,但这应该无关紧要,因为它们无论如何都是匿名的。查看 this and this link 了解更多信息,了解如何通过代码优先架构插入用户