无法对表执行创建、更新或删除操作,因为它没有主键

发布于 2024-07-16 07:07:12 字数 620 浏览 5 评论 0原文

我一直在尝试在具有标识列 RequestID (也是主键)的表中插入行,

    HelpdeskLog logEntry = new HelpdeskLog { RequestBody = message.Body };
    if (attachment != null)
        logEntry.Attachments = Helper.StreamToByteArray(attachment.ContentStream);
    Database.HelpdeskLogs.InsertOnSubmit(logEntry);

但我的代码不可避免地会引发以下错误

无法对表执行创建、更新或删除操作,因为它没有主键。

尽管主键列确实存在

这就是我试图做的:

  1. 在调试器中查看插入到对象模型中的标识列的值。 为 0
  2. 手动(使用 SQL)将假值插入表中 - 工作正常,按预期生成标识值
  3. 确保 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:

  1. To look in debugger the value of identity column being inserted in object model. It is 0
  2. To insert manually (with SQL) fake values into table - works fine, identity values generated as expected
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

千寻… 2024-07-23 07:07:12

我的 C# 代码中也出现过这个问题,并意识到我忘记了 IsPrimaryKey 指定:

  [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)已经有一个主表,这也是需要的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:

  [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; }
  }

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.

关于从前 2024-07-23 07:07:12

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

沫尐诺 2024-07-23 07:07:12

删除该表,然后重新插入。 在执行此操作之前,您必须确保该字段旁边有一个小钥匙。 重新编译您的项目,一切都会好起来的。

仅仅因为您更新了数据库并不意味着 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.

又怨 2024-07-23 07:07:12

由于该表在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文