C# EF 6代码优先与自定义存储过程
我正在使用代码优先的方法创建一个MVC 5应用程序,但我也在SQL Server数据库上创建了一些存储过程,有没有办法在创建数据库时也在c#中生成这些存储过程,可能是通过执行SQL脚本,如果有,我应该在哪里执行呢?您可能需要使用迁移来处理它。可以找到一个好看的解决方案。使用资源,但我相信,如果不想使用资源路径,您可以用相同的方式从.sql文件中读取文本。我会使用代码迁移 在Nuget软件包管理器中,您可以通过键入C# EF 6代码优先与自定义存储过程,c#,sql-server,asp.net-mvc,entity-framework,stored-procedures,C#,Sql Server,Asp.net Mvc,Entity Framework,Stored Procedures,我正在使用代码优先的方法创建一个MVC 5应用程序,但我也在SQL Server数据库上创建了一些存储过程,有没有办法在创建数据库时也在c#中生成这些存储过程,可能是通过执行SQL脚本,如果有,我应该在哪里执行呢?您可能需要使用迁移来处理它。可以找到一个好看的解决方案。使用资源,但我相信,如果不想使用资源路径,您可以用相同的方式从.sql文件中读取文本。我会使用代码迁移 在Nuget软件包管理器中,您可以通过键入 add-migration AddMyStoredProcedure 这将生成一
add-migration AddMyStoredProcedure
这将生成一个空类,如下所示
public partial class AddMyStoredProcedure : DbMigration
{
public override void Up()
{
}
public override void Down()
{
}
}
您所需要做的就是像这样添加存储过程(记住在Down方法中删除存储过程,以防将来需要回滚迁移)
最后更新你的数据库
update-database
迟交的答案,但也许有人会得到这样的问题的答案 在我的项目中,我有大量的
视图
和函数
和存储过程
,我使用的解决方案如下:
sql
文件以删除所有视图、函数和过程(如果它们存在),例如,将其称为drop.sql
视图
和函数
和存储过程
创建一个单独的sql
文件Embedded Resource
,右键单击文件属性,然后单击Build Action,选择Embedded Resource
YourFile.sql=>右键单击=>Properties=>Build操作,选择Embedded Resource
ExecuteSqlCommand
,您需要一种方法来读取这些文件以及下面所需的所有代码-- your views
if object_id('dbo.[YourViewName1]') is not null
drop view dbo.[YourViewName1]
if object_id('dbo.[YourViewName2]') is not null
drop view dbo.[YourViewName2]
-- your functions
if object_id('dbo.[Function1]') is not null
drop function dbo.[Function1]
if object_id('dbo.[Function2]') is not null
drop function dbo.[Function2]
-- your procedures
if object_id('dbo.[Procedure1]') is not null
drop procedure dbo.[Procedure1]
if object_id('dbo.[Procedure2]') is not null
drop procedure dbo.[Procedure2]
create view View1
as
select Field1,Field2,...Fieldn
from Table1
inner join Table2 on Id1 = FId2
inner join TableN on IdI = IdJ
view.sql或function.sql或procedure.sql结构
-- your views
if object_id('dbo.[YourViewName1]') is not null
drop view dbo.[YourViewName1]
if object_id('dbo.[YourViewName2]') is not null
drop view dbo.[YourViewName2]
-- your functions
if object_id('dbo.[Function1]') is not null
drop function dbo.[Function1]
if object_id('dbo.[Function2]') is not null
drop function dbo.[Function2]
-- your procedures
if object_id('dbo.[Procedure1]') is not null
drop procedure dbo.[Procedure1]
if object_id('dbo.[Procedure2]') is not null
drop procedure dbo.[Procedure2]
create view View1
as
select Field1,Field2,...Fieldn
from Table1
inner join Table2 on Id1 = FId2
inner join TableN on IdI = IdJ
迁移种子方法
private static string Load(string name)
{
var assembly = Assembly.GetExecutingAssembly();
using (Stream stream = assembly.GetManifestResourceStream(name))
using (StreamReader reader = new StreamReader(stream))
{
string result = reader.ReadToEnd();
return result;
}
}
我假设您在sql文件夹中创建了所有sql文件
在项目中的“迁移”文件夹内
最后是加载方法
private static string Load(string name)
{
var assembly = Assembly.GetExecutingAssembly();
using (Stream stream = assembly.GetManifestResourceStream(name))
using (StreamReader reader = new StreamReader(stream))
{
string result = reader.ReadToEnd();
return result;
}
}
此解决方案的好处是每次都会运行,而您将
确保是否存在任何问题(例如,在一段时间后,您更改了字段名或删除了视图或视图中使用的表)
函数或过程如果不记得必须更新过程,则会出现错误,如果启用了自动迁移,则可以进行修复)
希望这将帮助您复制