简体   繁体   中英

Value Insert error in LINQ/C# for Column identity

The Below code i used. SQL table name: tbl_Student
Columns : student_id (identity), Firstname, Lastname, Location, Email.

When i submit the below code. The following error will occur. I am new to this concept. I don't know how to manage the identity column. pls help.

Can't perform Create, Update or Delete operations on 'Table(tbl_Student)' because it has no primary key in c#

        tbl_Student ts = new tbl_Student();
        ts.Firstname = textBox1.Text;
        ts.Lastname = textBox2.Text;
        ts.Location = textBox3.Text;
        ts.Email = textBox4.Text;
        db.tbl_Students.InsertOnSubmit(ts);
        db.SubmitChanges();

As specified :

Can't perform Create, Update or Delete operations on a table because it has no primary key in c#

You will either need to try executing the command yourself ( via ExecuteCommand ), or better yet - modify your tables to have a key , which is highly recommended.

Note: As I specified in the comments below, after adding the key, regeneration of the models is needed.

Example for executing a query yourself:

db.ExecuteQuery<Studnet>("INSERT INTO tbl_Student VALUES(Firstname ,LastName, Location, Email)");

Did you have a primary key in the database table when you generated the Linq to SQL classes through the designer? Just to make sure, regenerate these classes through the designer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM