当实际插入发生在而不是触发器中时,使用LINQ to SQL获取插入的标识值 [英] Get inserted identity value with LINQ to SQL when the actual insert happens in an instead of trigger

查看:80
本文介绍了当实际插入发生在而不是触发器中时,使用LINQ to SQL获取插入的标识值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况
我有一张带有身份PK的表。在那张桌子上,我实现了(出于某些原因)而不是插入触发器。因此触发器执行实际插入。
插入LINQ后尝试使用scope_identitiy()检索刚刚插入的标识值。因为scope_identity()在这种情况下返回NULL(而不是刚刚插入的标识值)LINQ尝试将NULL分配给我的.NET类型"long"。当然,这失败了。实际上似乎这两个概念("而不是插入触发器"和"scope_identity()")不能一起工作......

问题
1)是否可以在执行而不是触发器中控制(赋值?)scope_identity。我希望LINQ通过后续调用scope_identity来接收正确的值...
2)我可以告诉LINQ使用另一种方法来检索标识值吗?哪种方式可以成为真正的替代方案?

请不要回答"为什么要使用而不是触发......"。情况给出了,我需要知道是否有一个好的解决方案。谢谢!

解决方案

嗨fcavelti,


< p class = MsoNormal>


好吧,据我所知,LINQ to SQL不能做一些像数据库中的触发器这样的功能,例如,当插件发生时,仅检测操作并检索插入值的方法是在数据库中插入触发器,LINQ to SQL不能这样做,没办法,它只是用于到目前为止在.Net应用程序中执行一些SQL Server数据库操作。




问候,


Xun


Situation
I have a table with an Identity PK. On that table I implemented (for some good reasons) an instead of insert trigger. So the trigger does the actual insert.
After the insert LINQ tries to retrieve the just inserted identity value using scope_identitiy(). Because scope_identity() returns NULL in that case (instead of the just inserted identity value) LINQ tries to assign NULL to my .NET type "long". And of course that fails. Actually it seems that these two concepts ("instead of insert trigger" an "scope_identity()") do not work together...

Questions
1) Is it possible to control (assign?) scope_identity within the execution of the instead of trigger. I want LINQ to receive the right value through its subsequent call to scope_identity...
2) Can I tell LINQ to use another way to retrieve the identity value? Which way would be a real alternative?

Please no answers like "why do you use instead of trigger...". The situation is given, I need to know if there is a good solution. Thanks!

解决方案

Hi fcavelti,


Well, as far as I aware, LINQ to SQL cannot do some feature like trigger in database, for example, when a insertion was occurring, the merely approach to detect the operation and retrieve the inserted value is insert trigger in database, LINQ to SQL cannot do this, no way, it is just used for doing some SQL server database operation in your .Net application so far.


Regards,

Xun


这篇关于当实际插入发生在而不是触发器中时,使用LINQ to SQL获取插入的标识值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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