首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CodeFirst调用存储过程

问CodeFirst调用存储过程
EN

Stack Overflow用户
提问于 2014-10-01 21:04:49
回答 1查看 376关注 0票数 1

我正在为我的程序使用CodeFirst和存储库模式。我在上下文中使用的数据库有一个在中创建和测试的名为dbo.sp_InsertTrackingInfo的存储过程。

在我的基本存储库类中

代码语言:javascript
复制
public IEnumerable<T> ExecWithStoreProcedure(string query, int id)
{
    return _context.Database.SqlQuery<T>("sp_InsertTrackingInfo @estimate", new SqlParameter("estimate", id));
}

在management中使用此查询,其中估计值被定义为整数。

代码语言:javascript
复制
EXEC sp_InsertTrackingInfo @estimate = '14'

除此之外,所有操作都正常工作,因此我知道我所指向的位置是对的,我已经检查了,并且在调用此函数的任何时候都没有调用此过程。

有人有这方面的经验吗?

谢谢,Bmckie

编辑:

在一个存储库中

代码语言:javascript
复制
using (var uow = UnitOfWorkManager.Begin())
{
    uow.EstimateTrackingRepository.Insert(t);
    uow.EstimateTrackingRepository.ExecWithStoreProcedure("exec sp_InsertTrackingInfo @estimate", t.EstimateId);
    uow.Commit();
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-01 21:26:23

如果该过程只执行insert语句,则可以使用ExecuteSqlCommand。

代码语言:javascript
复制
int rowsAffected = _context.Database
    .ExecuteSqlCommand("sp_InsertTrackingInfo {0}", id);

如果它执行select语句,则需要执行查询,因为它是延迟(延迟)执行,直到有需要时才执行。

代码语言:javascript
复制
var items = _context.Database
    .SqlQuery<T>("sp_InsertTrackingInfo {0}", id)
    .ToArray(); // or FirstOrDefault() or Any() etc
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26150639

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档