我想使用 linq to sql 在数据库中插入新行

发布于 2024-12-05 12:05:30 字数 470 浏览 2 评论 0原文

我尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幼儿园老大 2024-12-12 12:05:30

您需要确保模型中标识列的“自动生成值”属性设置为 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文