【问题标题】:How to use existing stored procedure & functions with Entity Framework Code-first如何使用实体框架代码优先的现有存储过程和函数
【发布时间】:2017-01-26 02:24:13
【问题描述】:

我正在使用EntityFramework.Functions 库,当我们使用实体框架代码优先时,该库用于调用存储过程或函数。

我有一个存储过程,它返回一个可为空的 long 值。但我得到一个错误:

概念模型不支持将方法 AddRecord_SP 的 System.Nullable`1[[System.Int64, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] 作为结构类型。

函数在我的DataContext 类中声明:

[Function(FunctionType.StoredProcedure, "Sp_name", Schema = "acnt")]
public virtual ObjectResult<Nullable<long>> AddRecord_SP(string i_Params)
{
     var i_ParamsParameter = i_Params != null ?
            new ObjectParameter("I_Params", i_Params) :
            new ObjectParameter("I_Params", typeof(string));

     return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<Nullable<long>>("Sp_name", i_ParamsParameter);
}

【问题讨论】:

  • 请添加调用AddRecord_SP的代码以获取更多详细信息。
  • 我没有任何代码调用这个方法。我在 datacontext 类中只有一个 SP 结构的定义

标签: c# asp.net-mvc entity-framework entity-framework-4 entity-framework-6


【解决方案1】:

据我所知,存储过程实际上不能指定参数不可为空。

可能你可以尝试指定返回参数。

 [return: Parameter(DbType = "")]

以下是我的项目中的示例代码,您可以参考。

[Function(FunctionType.StoredProcedure, "GetTop100", Schema = "biz")]
    public IQueryable<GetTop100Emp> GetTop100EmpList(long? FKTenant,  int IsFilter)
    {
        var parameters = new ObjectParameter[] {
            new ObjectParameter("FKTenant", FKTenant),
            new ObjectParameter("IsFilter", IsFilter),
        };
        return this.ObjectContext.ExecuteFunction<GetTop100Emp>("GetTop100", parameters).AsQueryable<GetTop100Emp>();

【讨论】:

    猜你喜欢
    • 2018-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多