c# - How to insert a new record which has a auto increment number as primary key? -
for example, have following table:
customerinfo
cus_id: auto increment, int, identity cus_name: nvarchar
if use follow codes insert record "peter",
string name = "peter"; datacontext dc = new datacontext(); customerinfo newcustomer = new customerinfo(); newcustomer.cus_name = name; dc.customerinfos.insertonsubmit(newcustomer); dc.submitchanges();
the following error returns,
can't perform create, update, or delete operations on 'table(customerinfo)' because has no primary key.
do need self-define cus_id
or other solutions? thanks!
first of linq-to-sql needs primary keys in order able inserts , updates, have add primary key in table.
now, because auto incremented identity column, in dbml, have select column "cus_id" of "customerinfo" table , go properties , set following:
- auto generated value : true
- auto-sync : oninsert
this ensure when insert new row new id.
Comments
Post a Comment