无法对表执行创建、更新或删除操作,因为它没有主键
我一直在尝试在具有标识列 RequestID (也是主键)的表中插入行,
HelpdeskLog logEntry = new HelpdeskLog { RequestBody = message.Body };
if (attachment != null)
logEntry.Attachments = Helper.StreamToByteArray(attachment.ContentStream);
Database.HelpdeskLogs.InsertOnSubmit(logEntry);
但我的代码不可避免地会引发以下错误
无法对表执行创建、更新或删除操作,因为它没有主键。
尽管主键列确实存在
这就是我试图做的:
- 在调试器中查看插入到对象模型中的标识列的值。 为 0
- 手动(使用 SQL)将假值插入表中 - 工作正常,按预期生成标识值
- 确保 SQLMetal 是否正确生成了表映射。 一切正常,主键属性已正确生成
,但是,这两种方法都没有帮助。 有什么技巧吗,有人知道吗?
I've been trying to insert row in the table having an identity column RequestID (which is primary key as well)
HelpdeskLog logEntry = new HelpdeskLog { RequestBody = message.Body };
if (attachment != null)
logEntry.Attachments = Helper.StreamToByteArray(attachment.ContentStream);
Database.HelpdeskLogs.InsertOnSubmit(logEntry);
But my code inevitably throws following error
Can't perform Create, Update or Delete operations on Table because it has no primary key.
despite primary key column exists indeed
That's what I tried to do:
- To look in debugger the value of identity column being inserted in object model. It is 0
- To insert manually (with SQL) fake values into table - works fine, identity values generated as expected
- To assure if SQLMetal has generated table map correctly . All OK, primary key attribute is generated properly
Nevertheless, neither of approaches helped. What's the trick, does anybody know?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我的 C# 代码中也出现过这个问题,并意识到我忘记了 IsPrimaryKey 指定:
即使您的数据库表(在本例中为 MySessionEntries)已经有一个主表,这也是需要的key 已定义,因为除非您使用 linq2sql 工具将数据库定义拉入 Visual Studio,否则 Linq 不会自动发现该事实。
I've also had this problem come up in my C# code, and realized I'd forgotten the IsPrimaryKey designation:
this is needed even if your database table (MySessionEntries, in this case) already has a primary key defined, since Linq doesn't automagically find that fact out unless you've used the linq2sql tools to pull your database definitions into visual studio.
LINQ 不允许向没有主键的表中插入数据。 要实现在没有主键的表中插入数据,您可以使用存储过程或创建查询并使用 LINQ 执行。 下面的链接提供了很好的解释。
无法对表(Employee)执行创建、更新或删除操作,因为它没有主键
LINQ does not allow to insert data into table without primary key. To achieve the insert data with table without primary key you can either use store procedure or create a query and execute using LINQ. Below link provide good explanation of the same.
Can't perform Create, Update or Delete operations on Table(Employee) because it has no primary key
删除该表,然后重新插入。 在执行此操作之前,您必须确保该字段旁边有一个小钥匙。 重新编译您的项目,一切都会好起来的。
仅仅因为您更新了数据库并不意味着 DBML 文件会以某种方式自动更新。 抱歉,事实并非如此。
Delete the table and then reinsert it. You must make sure there is a little small key next to the field before you do this. Recompile your project and all should be fine.
Just because you updated the dabase does not mean the DBML file somehow automatically updated. It does not, sorry.
由于该表在 SQL Server 中具有主键,因此在 linq2sql 设计器中重新添加该表。
如果情况并非如此,您可以在设计器上手动配置哪些属性是主键的一部分。
As the the table has the primary key in SQL Server, re-addthe table in the linq2sql designer.
If that were not the case, you can configure which properties are part of the primary key by hand on the designer.