Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using EF code-first while retaining stored procedures

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?

like image 226
Bill Avatar asked Mar 01 '26 16:03

Bill


1 Answers

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.

like image 177
Ladislav Mrnka Avatar answered Mar 04 '26 05:03

Ladislav Mrnka