Can't perform Create, Update or Delete operations on Table because it has no primary key

后端 未结 4 1094
情歌与酒
情歌与酒 2020-12-10 11:10

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 {          


        
相关标签:
4条回答
  • 2020-12-10 11:18

    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.

    0 讨论(0)
  • 2020-12-10 11:27

    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.

    0 讨论(0)
  • 2020-12-10 11:30

    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.

    0 讨论(0)
  • 2020-12-10 11:36

    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

    0 讨论(0)
提交回复
热议问题