iCediCe
iCediCe

Reputation: 1722

Stored procedures in EF 6.1.3

I'm new to Entity Framework and have ran into a problem. I have created a stored procedure in the DB (in SMMS) and triggering from .NET works fine.

However I would like EF to feed the SP to the database if the DB is wiped (like the seed method fills in data). Also I would like to have my SP in vertion control with the rest of my codebase. So, how do I add the SP to my project in a way that it will be created with the DB, I guess its like a migration, but with no direct connection to a model class.

Is it possible to generate a "empty" migration and then modify it to my needs? with up and down method that creates and removes the SP?

Upvotes: 0

Views: 102

Answers (1)

Colin
Colin

Reputation: 22595

Yes it is possible to generate an empty migration and then modify it to your needs. In package manager Add-Migration "AddMyStoredProcedure"

Then in your Up method:

Sql(@"EXECUTE('CREATE PROCEDURE MyStoredProcedure...etc ");

And in your Down method:

Sql(@"EXECUTE('DROP PROCEDURE MyStoredProcedure')");

I tend to use the Sql method, but there are also CreateStoredProcedure and DropStoredProcedure methods in the DbMigration class that you may prefer

Upvotes: 1

Related Questions