实体框架6 - 将表值参数传递给表值函数,返回IQueryable

时间:2018-01-27 17:38:11

标签: entity-framework-6 user-defined-functions table-valued-parameters

我想从实体框架中调用一个表值函数,并让它返回一个IQueryable。我的代码是:

// Defines table-valued function, which must return IQueryable<T>.
[Function(FunctionType.TableValuedFunction, nameof(ufnGetContactInformation), Schema = dbo)]
public IQueryable<ContactInformation> ufnGetContactInformation(
    [Parameter(DbType = "int", Name = "PersonID")]int? personId)
{
    ObjectParameter personIdParameter = personId.HasValue
        ? new ObjectParameter("PersonID", personId)
        : new ObjectParameter("PersonID", typeof(int));

    return this.ObjectContext().CreateQuery<ContactInformation>(
        $"[{nameof(this.ufnGetContactInformation)}](@{nameof(personId)})", personIdParameter);
}

但是,我也希望能够将一个表值参数传递给TVF(因此我可以将一个Id列表传递到TVF中)。我找到了可以使用DataTable执行此操作的代码,该DataTable引用了我在db中的用户定义类型,例如:

var dt = new DataTable();
dt.Columns.Add("Id");
dt.Rows.Add(1);
dt.Rows.Add(2);
dt.Rows.Add(3);

var idList = new SqlParameter("idList", SqlDbType.Structured);
warnings.Value = dt;
warnings.TypeName = "dbo.udt_idList";

entities.ExecuteStoredProcedure("usp_TableValuedFunc", idList);

问题是,第二个代码使用SQLParameter,第一个代码使用ObjectParameter。似乎ObjectParameter不允许提供用户定义类型,并且任何接受SQLParameter的代码都不允许我从TVF返回IQueryable。任何想法,我错过了什么?

0 个答案:

没有答案