SQL Server How to use table hints in LINQ to SQL

7qhs6swi  于 2023-10-15  发布在  其他
关注(0)|答案(2)|浏览(77)

How can I use table hints in LINQ to SQL when calling submit changes method?

dataContext.table2.something = dataContext.table1.something;
dataContext.SubmitChanges();

I want to generate something like this SQL code:

declare @var int;
begin transaction
select @var = something from table1 with (HoldLock);
update table2 set something = @var;
update table1 set something = @var + 1;
commit transaction;
j2qf4p5b

j2qf4p5b1#

This is not possible.

Actually, it is possible by doing serious nasty hacking using reflection. You can compile a query, and then fiddle with the generated SQL string in some internal object. This is the least desirable way to do this.

I recommend you stay with raw SQL for this one.

csbfibhn

csbfibhn2#

I've always heard that cant be done. Linq's goal (or at least one of them) is take or mind out of SQL so you dont have to worry with things like this. I suggest that you add your code with the table hint to a SQL procedure and use Linq to call it.

相关问题