ADO.NET Entity Framework Custom SQL Expressions for Attachments / Updates / Deletes

On the ScottGu blog, he shows how to override Linq to SQL class methods so that data changes can be overridden on the .NET side.

http://weblogs.asp.net/scottgu/archive/2007/08/27/linq-to-sql-part-8-executing-custom-sql-expressions.aspx (Custom SQL Expressions for Insert / Update / Remove part )

Is there a way to accomplish the same functionality in EF?

+2


source to share


2 answers


For EF 3.5, you don't really have an option other than sprocs.

But for EF 4.0, we added a new method to ObjectContext

called ExecuteStoreCommand(..)

and related methods, etc.

So you can override SaveChanges()

, it is now virtual and integrate ObjectStateManager

looking ObjectStateEntries

for the types (types) you are interested in that are in the EntityState you are interested in (i.e. Inserted), etc.

Once discovered, you can execute commands directly on the database using the new method ExecuteStoreCommand()

. Then, by calling AcceptChanges()

on ObjectStateEntry

, you stop EF from trying to flush the changes to the database that you have already processed.



You can then let EF do the rest of the changes for you by calling base.SaveChanges()

.

I know this is not ideal. But this is the best workaround I can think of

Alex

+6


source


EF allows you to map these operations to stored procedures in the mapping tab in the designer. Then you can map the input and output values ​​of the stored procedure to different properties of the object.



Hope it helps,

+1


source







All Articles