Fabio
Fabio

Reputation: 3120

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. To achieve this, EF needs to generate the following example for a DELETE statement.

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. 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.

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. 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...

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.

Upvotes: 0

Views: 1711

Answers (1)

podiluska
podiluska

Reputation: 51494

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.

Upvotes: 2

Related Questions