我正在学习 Linq-to-SQL。我想添加一条记录,但在以下方面出现异常InsertOnSubmit()
:
无法对“表(GuestbookEntry)”执行创建、更新或删除操作,因为它没有主键。
但是我的数据库确实有一个主键。
这是我的代码:
[Table(Name = "GuestbookEntry")]
public class GuestbookEntry
{
[Column(DbType = "int not null, IsPrimaryKey=true, IsDBGenerated=true")]
public int Id { get; set; }
[Column]
public string Name { get; set; }
[Column]
public string Message { get; set; }
[Column]
public DateTime DateAdded { get; set; }
}
public class GuestbookContext : DataContext
{
public Table<GuestbookEntry> GuestBookEntries;
//public Table<Order> Orders;
public GuestbookContext(string connection) : base(connection) { }
}
...
private GuestbookContext dbGuestbook = new GuestbookContext("GuestBookConnection");
...
dbGuestbook.GuestBookEntries.InsertOnSubmit(entry);
dbGuestbook.SubmitChanges();
你能告诉我有什么问题吗?
谢谢。