gpt4 book ai didi

asp.net-mvc - 元数据集合中不存在标识为 ' ' 的成员。\r\n参数名称 : identity

转载 作者:行者123 更新时间:2023-12-01 22:17:41 27 4
gpt4 key购买 nike

我在尝试调试时稍微简化了代码:

[HttpPost]
public ActionResult Register(User model)
{
DateTime bla = new DateTime(2012, 12, 12);
try
{
User user = new User
{
gid = 1,
cid = 1,
firstName = model.firstName,
lastName = model.lastName,
email = model.email,
username = model.username,
password = model.password,
creationDate = bla,
active = 1
};
myContext.Users.AddObject(user);
myContext.SaveChanges();

}
catch (Exception ex)
{
throw ex;
}

return View();
}

相应地传输值。用户表:

[id] [int] IDENTITY(1,1) NOT NULL,
[cid] [int] NULL,
[gid] [int] NULL,
[firstName] [nvarchar](100) NOT NULL,
[lastName] [nvarchar](100) NOT NULL,
[email] [nvarchar](max) NOT NULL,
[username] [nvarchar](100) NOT NULL,
[password] [nvarchar](100) NOT NULL,
[creationDate] [datetime] NOT NULL,
[active] [int] NOT NULL,

约束[PK_用户_3213E83F0AD2A005]主键聚集

我删除了所有外键以确保没有任何影响它。我非常确定在之前它是有效的,但现在我不知道问题出在哪里。执行保存更改时崩溃:

{"An error occurred while updating the entries. See the inner exception for details."}
{"The member with identity '' does not exist in the metadata collection.\r\nParameter name: identity"}

最佳答案

当我尝试使用 EF 插入时,我遇到了同样的错误,错误是

元数据集合中不存在身份为“Id”的成员。\r\n参数名称:identity

一开始并不明显,但异常消息非常简洁,因为我的数据库知道列 Id int 但在我的代码上为对象创建的属性是 int ID 回到命名映射,Id 没有映射到 ID。

因此,当将具有属性 ID 的对象发送到仅知道 Id 的数据库时,您将收到上述错误。

希望对您有帮助,谢谢

关于asp.net-mvc - 元数据集合中不存在标识为 ' ' 的成员。\r\n参数名称 : identity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10818730/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com