【发布时间】:2014-10-01 21:04:49
【问题描述】:
我正在为我的程序使用 CodeFirst 和存储库模式。我用作上下文的数据库有一个我在 SQL Server Management Studio 中创建和测试的存储过程,名为 dbo.sp_InsertTrackingInfo。
在我的基础存储库类中,我有
public IEnumerable<T> ExecWithStoreProcedure(string query, int id)
{
return _context.Database.SqlQuery<T>("sp_InsertTrackingInfo @estimate", new SqlParameter("estimate", id));
}
在管理工作室中使用此查询有效,其中估计定义为整数
EXEC sp_InsertTrackingInfo @estimate = '14'
除此之外的所有操作都可以正常工作,因此我知道我指向了正确的位置,我检查了 SQL Server Profiler,并且在调用此函数的任何时候都没有调用此过程。
有人有这方面的经验吗?
谢谢, 麦基
编辑:
在其中一个存储库中
using (var uow = UnitOfWorkManager.Begin())
{
uow.EstimateTrackingRepository.Insert(t);
uow.EstimateTrackingRepository.ExecWithStoreProcedure("exec sp_InsertTrackingInfo @estimate", t.EstimateId);
uow.Commit();
}
【问题讨论】:
-
哪个代码调用了
ExecWithStoreProcedure? -
我将其添加到编辑中
-
我认为您需要在
SqlQuery之后或在调用ExecWithStoreProcedure之后使用ToArray()之类的内容执行查询,该过程是否在插入语句之后执行选择查询?如果它只是一个插入语句,可能你可以使用ExecuteSqlCommand。 -
旁注:您应该不为您的存储过程使用
sp_前缀。微软有reserved that prefix for its own use (see Naming Stored Procedures),你确实会在未来某个时候冒着名称冲突的风险。 It's also bad for your stored procedure performance。最好只是简单地避免sp_并使用其他东西作为前缀 - 或者根本不使用前缀! -
另外:它是一个 stored 过程 - 就像在 SQL Server 中的 stored 中一样 - 它与 store 因此它不是 存储过程 ....
标签: c# sql sql-server stored-procedures ef-code-first