简体   繁体   English

实体框架和SQL Server OUTPUT子句

[英]Entity Framework and SQL Server OUTPUT clause

I'd like to use SQL OUTPUT clause to keep history of the records on my database while I'm using Entity Framework. 我想在使用Entity Framework时使用SQL OUTPUT子句来保存数据库中的记录历史记录。 To achieve this, EF needs to generate the following example for a DELETE statement. 为此,EF需要为DELETE语句生成以下示例。

Delete From table1
output deleted.*, 'user name', getdate() into table1_hist
Where field = 1234;

The table table1_hist has the same columns as table1, with the addition of two columns to store the name of the user who did the action and when it happened. 表table1_hist与table1具有相同的列,并添加了两列来存储执行操作的用户的名称及其发生的时间。 However, EF doesn't seem to have a way to support this SQL Server's clause, so I'm lost on how to implement that. 但是,EF似乎没有办法支持这个SQL Server的子句,所以我迷失了如何实现它。

I looked at EF's source code, and the DELETE command is create inside a internal static method (GenerateDeleteSql in System.Data.Entity.SqlServer.SqlGen.DmlSqlGenerator class) , so I can't extend the class to add the behavior I want. 我查看了EF的源代码, DELETE命令是在内部静态方法(GenerateDeleteSql in System.Data.Entity.SqlServer.SqlGen.DmlSqlGenerator class) ,因此我无法扩展该类以添加我想要的行为。 It looks like I'll have to rewrite the SQL Server provider based on the existing code, but that is something I'd like to avoid... 看起来我将不得不根据现有代码重写SQL Server提供程序,但这是我想要避免的...

So, my question is if there's another option to do this (an extension, for example) or do I have to rewrite this provider? 所以,我的问题是,是否还有另一种选择(例如扩展)或者我是否必须重写此提供程序?

Thank you. 谢谢。

Have you considered either 你考虑过吗?

  • Using Stored Procedures to encapsulate your data logic 使用存储过程封装数据逻辑
  • A delete trigger to capture the data 用于捕获数据的删除触发器
  • Change Data Capture (Enterprise edition only) 更改数据捕获(仅限企业版)
  • not actually deleting the data - merely setting a flag in the data to mark it as deleted. 实际上并没有删除数据 - 只是在数据中设置一个标志以将其标记为已删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM