DbContext中的数据库集表示数据库中的表。DbContext知道表之间的关系以及如何将LINQ查询转换为数据库能够理解的查询。DbContext的任务是隐藏数据库的内部。每当您想与数据库通信时,都要使用DbContext。
因此,DbContext是放置存储过程的好地方。由于DbContext还创建了模型(在DbContext.OnModelCreating中),因此它也是添加创建存储过程的功能的好地方。
DbContext的用户可能期望以下功能:
-
使用参数调用存储过程
-
使用参数async调用存储过程
(您的问题)
-
存储过程是否存在?
-
创建模型时创建或更改存储过程
DbContext将使用DbContext执行存储过程。数据库ExecuteSqlCommand。此函数具有异步等效项:
DbContext.Database.ExecuteSqlAsync
class MyDbContext : DbContext
{
// TODO: add DbSets
#region stored procedure
public void CallMyStoredProcedure(MyParams myParams)
{
object[] functionParameters = this.CreateFunctionParams(myParams);
this.Database.ExecuteSqlCommand(sqlCommandMyStoredProcedure, functionParameters);
}
public async Task CallMyStoredProcedure(MyParams myParams)
{
object[] functionParameters = this.CreateFunctionParams(myParams);
await this.Database.ExecuteSqlCommandAsync(
sqlCommandMyStoredProcedure,
functionParameters)
.ConfigureAwait(false);;
}
// TODO: add more functions
#endregion stored procedures
}
这些功能使用了其他几个功能:
// name of the stored procedure, names of the parameters:
private const string myStoredProcedureName = "InsertPoint";
private const string paramProductName = "ProductName";
private const string paramCount = "Count";
// SQL command to execute stored procedure with the parameters
private const string SqlCmndMyStoredProcedure = @"Exec "
+ myStoredProcedureName
+ @" @ParamProductName, @ParamCount";
private object[] CreateFunctionParams(MyParams myParams)
{
return newObject[]
{
new SqlParameter(paramProductName, myParams.ProductName),
new SqlParameter(paramCount, myParams.Count),
};
}
要完成收集,请添加一个检查存储过程是否存在的方法和一个创建存储过程的方法:
检查存储过程是否已存在
public bool MyStoredProcedureExists()
{
return this.StoredProcedureExists(myStoredProcedureName);
}
public bool StoredProcedureExists(string procedureName)
{
object[] functionParameters = new object[]
{
new SqlParameter(@"procedurename", procedureName),
};
string query = @"select [name] from sys.procedures where name= @procedurename";
return this.Database.SqlQuery<string>(query, functionParameters)
.ToList()
.Where(item => item == procedureName)
.Any();
}
创建存储过程:
public void CreateMyStoredProcedure(bool forceCreate)
{
// do not create if already exists, except if forceCreate:
bool storedProcedureExists = this.MyStoredProcedureExists;
if (!storedProcedureExists || forceCreate)
{ // create the stored procedure:
var x = new StringBuilder();
// decide whether to create or Alter
if (!storedProcedureExists)
{
x.Append(@"CREATE");
}
else
{
x.Append(@"ALTER");
}
// procedure name:
x.Append(@" PROCEDURE ");
X.AppendLine(myStoredProcedureName);
// parameters:
x.AppendLine(@"@ProductName NVARCHAR(80),"
X.AppendLine(@"@Count int")
// procedure code:
x.AppendLine(@"AS")
X.AppendLine(@"BEGIN")
... // TODO: add procedure code
x.AppendLine(@"END");
this.Database.ExecuteSqlComment(x.ToString());
}
}
最后,关于模型创建:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
this.CreateMyStoredProcedure(false); // don't force if already exists;
// TODO: add fluent API
}