Baig
Baig

Reputation: 1519

Is it possible to override the ObjectContext.SaveChanges method in entity Framework?

I was wondering if it is possible to override the ObjectContext.SaveChanges() method and write our own sql logic to save the changes made to the entities in the object context instead of relying on Entity Framework to save those changes in the database.

Upvotes: 0

Views: 2125

Answers (3)

Craig Stuntz
Craig Stuntz

Reputation: 126547

@Ladislav is correct that a stored proc is one way to do this (+1).

Another way is to write a wrapper provider.

Upvotes: 0

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364249

Generally you can do anything you want if you override SaveChanges and do not call base.SaveChanges but you will loose all the stuf EF will do for you. It means you will have to manually browse metadata and map your entities to SQL tables and columns. There will be like writing half the ORM yourselves.

If you just need some little custom logic when persisting entity you can map imported stored procedure to Insert, Update and Delete operations in the entity designer.

Upvotes: 3

Yury Tarabanko
Yury Tarabanko

Reputation: 45121

In EF4 SaveChanges(SaveOptions) is virtual. You can override this method. MSDN

Upvotes: 1

Related Questions