I´m creating a MVC 5 app with a Code-First approach, but I also created some stored procedures on the SQL Server database, is there a way to also generate these stored procedures in c# when the database is created, maybe by executing a sql script, if so where should I do this?
相关问题
- Sorting 3 numbers without branching [closed]
- sql execution latency when assign to a variable
- Graphics.DrawImage() - Throws out of memory except
- MVC-Routing,Why i can not ignore defaults,The matc
- Why am I getting UnauthorizedAccessException on th
I would use code migrations.
From your Nuget Package Manager you can set up a blank migration by typing
This should generate an empty class like so
All you need to do is add your stored procedure like so (remember to drop the stored procedure in the Down method in case you need to roll back the migration in the future).
Finally update your database
You will probably need to use Migrations to handle it. A good looking solution can be found https://stackoverflow.com/a/15171900/119262. using resources but I am sure you could just read the text out of your .sql files in the same manner if you didnt want to go down the resource path.
Late answer, but maybe someone will get an answer for such question
I had plenty of
views
andfunctions
andstored procedures
to deal with in my project, and the solution I used was as follows:sql
file in your project to drop all views and functions and procedures you have if they were exists,call itdrop.sql
for example.view
andfunction
andstored procedure
a separatesql
file in your project.Embedded Resource
, by right click on the file then property then Build Action , chooseEmbedded Resource
ExecuteSqlCommand
, you need a method to allow you to read these files and below all the code required.drop.sql structure
view.sql or function.sql or procedure.sql structure
Migration seed method
Finally the Load method
Hope this will help you