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

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

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

我如何导入我的function?

我不知道我在ExecuteFunction中看到了什么,但它无法正常工作。 我终于得到了一个端到端的解决方案以及这篇文章的帮助和其他回复中显示的文章。

第一步是将函数放入EDMX文件:

     

第二步是在与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."); } } } 

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

  using System.Data.Objects; protected string ProcessReplacements(Guid versionId, Guid surveyId, string input) { if (input == null) return null; List parameters = new List(3); parameters.Add(new ObjectParameter("VersionId", versionId)); parameters.Add(new ObjectParameter("SurveyId", surveyId)); parameters.Add(new ObjectParameter("Input", input)); var output = db.CreateQuery("SurveyDesignerModel.Store.ProcessReplacements(@VersionId, @SurveyId, @Input)", parameters.ToArray()) .Execute(MergeOption.NoTracking) .FirstOrDefault(); return output; } 

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

那是正确的。 您不能为SQL函数创建函数导入,而只能为SQL存储过程创建函数导入。 您可以将SQL函数导入存储模型,但必须手动创建方法来调用该函数:

 public static class EdmFunctions { [EdmFunction("TestModel.Store", "FunctionName")] public static string SomeName(string someParam) { throw new NotSupportedException("This function is only for L2E query."); } } 

EdmFunction中的命名空间必须是存储容器的名称空间(EDMX文件中的SSDL),名称必须是导入函数的名称。 在.NET代码中调用时,此方法没有意义。 因此,它会抛出exception。 它仅适用于转换为SQL = linq-to-entities的查询。

我有同样的问题,并成功解决了它。 有关此主题的原始MSDN文章如下: 如何:调用自定义数据库函数