我正在尝试使用stored procedure
来呼叫Linq
。为此,我写了这段代码:
public class CtxDb:DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
public virtual ObjectResult<Employee> GetEmployee()
{
return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<Employee>("GetEmployee");
}
}
当我从Web API
调用我的sp时,我收到错误:
在容器中找不到FunctionImport'GetEmployee' 'CtxDb'
答案 0 :(得分:1)
使用容器名称限定函数导入,如下所示:
public virtual ObjectResult<Employee> GetEmployee()
{
return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<Employee>("EntityContainerName.GetEmployee");
}
您可以在EDMX上找到实体容器名称 - 右键单击任意位置,然后执行&#34;属性&#34;。
替代方式:
public virtual ObjectResult<Employee> GetEmployee() {
return this.Database.SqlQuery<Employee>("GetEmployee");
}
答案 1 :(得分:0)
希望这对您有帮助
public virtual IEnumerable<GetProductCategoryList_Result> GetProductCategoryList(Nullable<int> userID)
{
var userIDParameter = userID.HasValue ?
new SqlParameter("UserID", userID) :
new SqlParameter("UserID", System.Data.SqlDbType.Int);
//return this.Database.SqlQuery<GetProductCategoryList_Result>("GetProductCategoryList @UserID", userIDParameter).ToList();
return ((IObjectContextAdapter)this).ObjectContext.ExecuteStoreQuery<GetProductCategoryList_Result>("osa.GetProductCategoryList @UserID", userIDParameter);
}