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

标签 asp.net-mvc linq ado.net

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

[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/

相关文章:

c# - 通过查询字符串将对象列表传递给 MVC Controller

c# - Entity Framework 级联删除使用linq

c# - 将 DataTable 转换为 LINQ : Unable to query multiple fields

c# - 如何从 SQL Server 获取 ado.net 中数字数据类型的值

c# - 为什么取消引用 DataRow 空值不会引发异常?

c# - ADO.NET SQL Server 性能 : multiple result sets vs. 多个命令执行

jQuery AJAX 将重定向视为状态 200 而不是 302?

javascript - 将数据从 View 传递到 Controller

c# - 为 Linq 中的分组项目添加编号

c# - 将 LINQ 与不同层一起使用意味着我无法访问特定类型