无法为可组合函数创建函数导入

Hel*_*lio 14 .net c# entity-framework

我为我的数据库对象生成了Entity CodeBlock,并选择了一些用户定义的标量函数.但是当我试图双击Model.Store中的函数来导入函数时,我得到了这个错误.

无法为可组合函数创建函数导入.

我如何导入我的功能?

Ben*_*ill 8

我不知道我在ExecuteFunction中看到了什么,但它无法正常工作.我终于从这个帮助有一个终端到终端的解决方案一起,并在其他答复显示的文章.

第一步是将功能放入EDMX文件:

    <Function Name="ProcessReplacements" ReturnType="nvarchar(max)" Aggregate="false" BuiltIn="false" NiladicFunction="false" IsComposable="true" ParameterTypeSemantics="AllowImplicitConversion" Schema="Data">
      <Parameter Name="VersionId" Type="uniqueidentifier" Mode="In" />
      <Parameter Name="SurveyId" Type="uniqueidentifier" Mode="In" />
      <Parameter Name="Input" Type="nvarchar(max)" Mode="In" />
    </Function>
Run Code Online (Sandbox Code Playgroud)

第二步是在与EDMX文件相同的命名空间中设置一个类(通过在与EDMX文件相同的目录中创建类来轻松完成:

using System.Data.Objects.DataClasses;
namespace Same.As.Edmx
{
    public static class EdmFunctions
    {
        [EdmFunction("SurveyDesignerModel.Store", "ProcessReplacements")]
        public static string ProcessReplacements(Guid VersionId, Guid SurveyId, string Input)
        {
            throw new NotSupportedException("Direct calls are not supported.");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

第三步是编写一个指向函数的对象查询:

    using System.Data.Objects;
    protected string ProcessReplacements(Guid versionId, Guid surveyId, string input)
    {
        if (input == null)
            return null;

        List<ObjectParameter> parameters = new List<ObjectParameter>(3);
        parameters.Add(new ObjectParameter("VersionId", versionId));
        parameters.Add(new ObjectParameter("SurveyId", surveyId));
        parameters.Add(new ObjectParameter("Input", input));

        var output = db.CreateQuery<string>("SurveyDesignerModel.Store.ProcessReplacements(@VersionId, @SurveyId, @Input)", parameters.ToArray())
            .Execute(MergeOption.NoTracking)
            .FirstOrDefault();

        return output;
    }
Run Code Online (Sandbox Code Playgroud)

对我而言,关键是对对象上下文的CreateQuery调用和"查询字符串"的语法.请注意对EDMX中定义的函数的完全限定引用,这是我缺少的链接:-)