Linq to SQL:提交到数据库之前修改命令
Linq to SQL 中是否有一个钩子允许我在命令发送到数据库之前手动修改命令?
具体来说,我试图介绍 WITH CHANGE_TRACKING_CONTEXT(@contextData)< /code>
语法每当插入/更新/删除应用于数据库时,以便能够针对更改跟踪添加附加信息,例如发起更改的用户(这是一个使用SQL 身份验证)。
只需将该语法插入 CommandText 的开头,并将附加参数 (@contextData)
添加到 SqlCommand 对象,该对象将包含附加语句的数据。
提前致谢。
Is there a hook somewhere within Linq to SQL that would allow me to manually modify a command before it is sent to the database?
Specifically, I am trying to introduce the WITH CHANGE_TRACKING_CONTEXT(@contextData)
syntax whenever an insert/update/delete is applied to the database in order to be able to add additional information against the change tracking, such as the user who instigated the change (this is a web application that uses SQL Authentication).
This syntax will simply need to be inserted at the beginning of the CommandText, along with an additional parameter (@contextData)
being added to the SqlCommand object which will contain the data for the additional statement.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要更改任何 SQL...只需为您的 DataContext 提供现有的 SqlConnection 即可。
http://msdn.microsoft.com/en-us/library/bb386986。 aspx
在通过 DataContext 提交更改之前.SubmitChanges,为
WITH CHANGE_TRACKING_CONTEXT
命令创建您自己的 SqlCommand(使用您创建 DataContext 所用的相同 SqlConnection)并执行它。该语句在数据库连接期间有效。更新:
因为这不起作用,并且如果手动进行更改跟踪不是一个选项(通过 DataContext.GetChangeSet 这是我在当前应用程序上所做的),我认为您将被困在做最丑陋的事情中: 指定 SQL通过 DataContext 上的部分方法执行所有 INSERT/UPDATE/DELETE 的语句(最后一部分)。或者更丑陋的是,使用 DataContext.Log 记录所有生成的 SQL,在事务中执行 SubmitChanges,回滚,然后修改从日志中获取的 SQL 并根据需要执行它。
You shouldn't need to alter any SQL... just supply your DataContext with an existing SqlConnection.
http://msdn.microsoft.com/en-us/library/bb386986.aspx
Before submitting changes via DataContext.SubmitChanges, create your own SqlCommand (using the same SqlConnection you created your DataContext with) for the
WITH CHANGE_TRACKING_CONTEXT
command and execute it. That statement will be valid for the duration of the database connection.UPDATE:
Since that didn't work, and if doing change tracking manually isn't an option (via DataContext.GetChangeSet which is what I do on my current app), I think you're going to be stuck doing just about the ugliest thing possible: specify the SQL statements for all of your INSERT/UPDATE/DELETE through partial methods on your DataContext (last section). Or even uglier, use DataContext.Log to log all the SQL generated, execute the SubmitChanges in a Transaction, roll it back, then modify the SQL you get out of the log and execute it as desired.