EF 6代码优先,具有自定义存储过程

编程入门 行业动态 更新时间:2024-10-28 00:22:57
本文介绍了EF 6代码优先,具有自定义存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用代码优先"方法创建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代码优先,具有自定义存储过程

本文发布于:2023-11-12 04:54:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1580563.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   存储过程   代码   EF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!