无法对表执行创建、更新或删除操作,因为它没有主键 [英] Can't perform Create, Update or Delete operations on Table because it has no primary key
问题描述
我一直在尝试在具有标识列 RequestID(也是主键)的表中插入行
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
无法对 Table 执行创建、更新或删除操作,因为它没有主键.
Can't perform Create, Update or Delete operations on Table because it has no primary key.
尽管主键列确实存在
这就是我试图做的:
- 在调试器中查看插入到对象模型中的标识列的值.是0
- 手动(使用 SQL)将假值插入表中 - 工作正常,生成的标识值符合预期
- 确保 SQLMetal 是否正确生成了表映射.一切OK,主键属性生成正确
尽管如此,这两种方法都没有帮助.有什么诀窍,有人知道吗?
Nevertheless, neither of approaches helped. What's the trick, does anybody know?
推荐答案
我的 C# 代码中也出现了这个问题,并意识到我忘记了 IsPrimaryKey 名称:
I've also had this problem come up in my C# code, and realized I'd forgotten the IsPrimaryKey designation:
[Table (Name = "MySessionEntries" )]
public class SessionEntry
{
[Column(IsPrimaryKey=true)] // <---- like this
public Guid SessionId { get; set; }
[Column]
public Guid UserId { get; set; }
[Column]
public DateTime Created { get; set; }
[Column]
public DateTime LastAccess { get; set; }
}
即使您的数据库表(MySessionEntries,在这种情况下)已经定义了主键,也需要这样做,因为除非您使用了 linq2sql 工具,否则 Linq 不会自动找出该事实将您的数据库定义拉入 Visual Studio.
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.
这篇关于无法对表执行创建、更新或删除操作,因为它没有主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!