插入后停止LINQ to SQL执行选择语句 [英] Stop LINQ to SQL from executing select statements after insert

查看:87
本文介绍了插入后停止LINQ to SQL执行选择语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用LINQ to SQL更新我的数据库.我正在插入很多记录,当我调用SubmitChanges()时,LINQ to SQL对每个对象执行一个插入和一条select语句.将对象插入数据库后,我真的不在乎更新它们.

I'm using LINQ to SQL to update my database. I'm inserting a lot of records, and when I call SubmitChanges(), LINQ to SQL executes an insert and a select statement for each object. I don't really care to update my objects after they are inserted into the database.

您知道我可以阻止LINQ to SQL在insert语句之后发出select语句吗?这应该会使我的应用程序更快.

Do you know I can prevent LINQ to SQL from issuing the select statements after the insert statements? This should make my app much faster.

推荐答案

您正在寻找

You're looking for ColumnAttribute.AutoSync. If you're using the designer, check each column for an Auto-Sync property and set it to Never.

修改: 好的,这对您不起作用.为自己的地图黑客做好准备!

Ok, that didn't work for you. Brace yourself for some mapping hackery!

当我插入带有一些自动生成的主键列时,我得到以下SQL:

When I Insert with some autogenerated primary key column, I get this SQL:

INSERT INTO [dbo].[TableName]( fieldlist )
VALUES (@p0, @p1, @p2, @p3, @p4)

SELECT CONVERT(Int,SCOPE_IDENTITY()) AS [value]

据我所知,您不希望执行SELECT

As I understand your request, you don't want that SELECT

尝试1:我去了主键字段,并将auto-generated设置为false.这导致了Sql异常当IDENTITY_INSERT设置为OFF时,无法在表'TableName'中为标识列插入显式值".换句话说,linq为该列指定了一个值.

Attempt 1: I went to the primary key field and set auto-generated to false. This caused a Sql Exception "Cannot insert explicit value for identity column in table 'TableName' when IDENTITY_INSERT is set to OFF." In other words, linq specified a value for that column.

尝试2:我从设计器中删除了自动生成的列.这导致Linq给我一个无效的操作异常:无法对'Table(TableName)'执行创建,更新或删除操作,因为它没有主键."

Attempt 2: I deleted the autogenerated columns from the designer. This caused Linq to give me an Invalid Operation Exception: "Can't perform Create, Update or Delete operations on 'Table(TableName)' because it has no primary key."

尝试3:我从设计器中删除了自动生成的列,然后将另一列标记为主键.即使此列不是数据库中的主键,LINQ的DataContext也会使用它来跟踪行标识.对于给定的DataContext观察到的记录,它必须是唯一的.

Attempt 3: I deleted the autogenerated columns from the designer, then I marked another column as primary key. Even though this column is not a primary key in the database, LINQ's DataContext will use it to track row identity. It must be unique for observed records of a given DataContext.

这第三次尝试生成了以下SQL(这就是您要的内容)

This third attempt generated the following SQL (which is what you ask for)

INSERT INTO [dbo].[TableName]( fieldlist )
VALUES (@p0, @p1, @p2, @p3, @p4)

这篇关于插入后停止LINQ to SQL执行选择语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆