views:

2404

answers:

4

Hello!

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?

+1  A: 

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.

eglasius
+2  A: 

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.

Al Katawazi
+4  A: 

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.

SteveL
A: 

well,that's cool. thank you.

taurusanger