3

I've started building a project using code-first in EF4.0 to build my database. I now wish to create some stored procedures in SQL Server 2008 that will work on the created data tables.

However, since CF drops and recreates the entire database, I would lose any stored procedures I create and would have to rebuild them each time - it would be easy to inadvertently lose work this way.

Are there "best practices" here? Of course I could always lock down the schema now and forget about code first but it's not ideal. Why would EF delete the entire DB - is there a way to delete just the tables?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Bill
  • 33
  • 4
  • Since EF code-first right now (EF 4.3) doesn't and in the near future (EF 5.0) still won't support stored procedures, I'm afraid there's not a lot you can do. But : you could look into [EF code-first **migrations**](http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-code-based-migrations-walkthrough.aspx) to avoid constantly dropping & re-creating your database - instead, you incrementally upgrade it as you need. This will leave your stored procedures intact. – marc_s Jul 25 '12 at 08:22

1 Answers1

1

You can use custom database initializer which will create your stored procedures every time you recreate database. You can also use EF migrations to build your database incrementally instead of deleting it after each change. Up method in the migration can use Sql method to create stored procedures.

Community
  • 1
  • 1
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670