我正在使用代码优先"方法创建MVC 5应用程序,但是我还在SQL Server数据库上创建了一些存储过程,有没有一种方法可以在创建数据库时也在c#中生成这些存储过程,也许通过执行sql脚本,如果可以的话,该在哪里做?
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?
推荐答案我将使用代码迁移.
在您的Nuget软件包管理器中,您可以通过键入以下内容来设置空白迁移
From your Nuget Package Manager you can set up a blank migration by typing
add-migration AddMyStoredProcedure这应该会生成一个空的类,
This should generate an empty class like so
public partial class AddMyStoredProcedure : DbMigration { public override void Up() { } public override void Down() { } }您需要做的就是像这样添加存储过程(请记住在Down方法中删除该存储过程,以防将来需要回滚迁移时使用该存储过程.
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).
public partial class AddMyStoredProcedure : DbMigration { public override void Up() { Sql(@" CREATE PROCEDURE dbo.GetMyAddress AS SELECT * FROM Person.Address"); } public override void Down() { Sql("DROP PROCEDURE dbo.GetMyAddress"); } }最后更新您的数据库
update-database更多推荐
EF 6代码优先,具有自定义存储过程
发布评论