值在LINQ/C#中为列标识插入错误
本文关键字:标识 插入 错误 LINQ 值在 | 更新日期: 2023-09-27 18:21:38
我使用的以下代码。SQL表名称:tbl_Student
列:student_id(身份)、名字、姓氏、位置、电子邮件。
当我提交以下代码时。将出现以下错误。我对这个概念是陌生的。我不知道如何管理标识栏。请帮忙。
无法对执行创建、更新或删除操作'Table(tbl_Student)',因为它在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();
如指定:
Can't perform Create, Update or Delete operations on a table because it has no primary key in c#
您将需要尝试自己执行该命令(通过ExecuteCommand
),或者更好地-修改您的表,使其具有键,这是强烈建议的。
注意:正如我在下面的评论中所指出的,在添加密钥后,需要重新生成模型
自己执行查询的示例:
db.ExecuteQuery<Studnet>("INSERT INTO tbl_Student VALUES(Firstname ,LastName, Location, Email)");
当您通过设计器生成Linq-to-SQL类时,数据库表中是否有主键?只是为了确保,通过设计器重新生成这些类。