我想使用 linq to sql 在数据库中插入新行
我尝试使用 linq to sql 在数据库的项目表中添加新项目,如下所示:
Items new_item = new Items();
new_item.Title = editproductid.Text;
new_item.Path = @"~\images\" + uploadimage.FileName;
StaticVa.new_data_context.Items.InsertOnSubmit(new_item);
StaticVa.new_data_context.SubmitChanges();
但出现异常
无法在表“Items”中插入标识列的显式值 IDENTITY_INSERT 设置为 OFF。
我怎样才能打开它?或者还有其他插入方式吗?
I tried to add new items in items table in database using linq to sql like this :
Items new_item = new Items();
new_item.Title = editproductid.Text;
new_item.Path = @"~\images\" + uploadimage.FileName;
StaticVa.new_data_context.Items.InsertOnSubmit(new_item);
StaticVa.new_data_context.SubmitChanges();
but I got an exception
Cannot insert explicit value for identity column in table 'Items' when
IDENTITY_INSERT is set to OFF.
how can I open it ? or are there another ways to insert ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要确保模型中标识列的“自动生成值”属性设置为 True,以便 Linq to Sql 知道不要尝试插入值本身。
You need to make sure that the "Auto Generate Value" property in your model for the identity column is set to True, so that Linq to Sql knows to not try to insert a value itself.