Hari
Hari

Reputation: 137

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();

Upvotes: 0

Views: 241

Answers (2)

Ron.B.I
Ron.B.I

Reputation: 2766

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)");

Upvotes: 1

Tarik
Tarik

Reputation: 11209

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.

Upvotes: 0

Related Questions