简体   繁体   English

EF 6.1.3中的存储过程

[英]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. 我已经在数据库中(在SMMS中)创建了一个存储过程,并且从.NET触发可以正常工作。

However I would like EF to feed the SP to the database if the DB is wiped (like the seed method fills in data). 但是,如果数据库被擦除(例如,seed方法填充数据),我希望EF将SP馈送到数据库。 Also I would like to have my SP in vertion control with the rest of my codebase. 我也想让我的SP与其余代码库一起进行版本控制。 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. 因此,如何将SP添加到我的项目中,使其可以与数据库一起创建,我想它就像迁移一样,但是没有与模型类的直接连接。

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? 使用上下方法创建和删除SP?

Yes it is possible to generate an empty migration and then modify it to your needs. 是的,可以生成一个空迁移,然后根据需要对其进行修改。 In package manager Add-Migration "AddMyStoredProcedure" 在包管理器中, Add-Migration "AddMyStoredProcedure"

Then in your Up method: 然后在您的Up方法中:

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

And in your Down method: 在您的Down方法中:

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 我倾向于使用Sql方法,但您可能更喜欢DbMigration类中的CreateStoredProcedureDropStoredProcedure方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM