0

i use the entity framework code first for my application and i need a trigger. My Application should support different database engines like

  • Sql Server
  • SqlServerCE
  • SqlExpress

Trigger, Stored procs are not supported in SqlServerCE, what would you do to get this functionality?

I could do something after "SaveChanges" or something, what is a good way ?

1
  • At first glance I thought this was about EF Codd :) Commented Nov 3, 2011 at 9:49

2 Answers 2

1

Yes you can do something inside SaveChanges (by overriding) or after SaveChanges and call SaveChanges again to persist new data but it is not exactly the same as trigger. Simply if your requirement is to use trigger for some functionality SqlServerCE is not a choice for you. If you rewrite the trigger logic to your application you should have two versions anyway - one for big SQL using triggers and one for SQL CE not using triggers.

Sign up to request clarification or add additional context in comments.

1 Comment

I think too. I need 2 "versions" depending on the database.
0

Code first, although it allows you to send in some raw queries and hence perform your own native database/server manipulations, basically is designed only for building a model and querying upon that model.

Now as for your question: You can directly build 'stored procedure alternatives' by adding methods to your DbContext (or extension methods for seperation of concerns).

Triggers are a little more complex. You can override the SaveChanges method in which you can basically track each change made either before it got persisted back to the database- or after.

Both problems however can also be solved by introducing a repository. See: http://huyrua.wordpress.com/2011/04/13/entity-framework-4-poco-repository-and-specification-pattern-upgraded-to-ef-4-1 This allows you to launch a script (trigger) when adding, updating or removing a certain entity

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.