Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function imports cannot be created for composable functions

I have generated Entity CodeBlock for my database objects and choose some of my user defined scalar functions. But when i tried to double click on functions in Model.Store to import function i get this error.

Function imports cannot be created for composable functions.

How can i import my functions?

like image 354
Helio Avatar asked May 03 '11 07:05

Helio


2 Answers

I don't know what I was seeing with the ExecuteFunction, but it wasn't working. I finally got an end-to-end solution together with help from this post and the article shown in the other responses.

Step one is to get the function into your EDMX file:

    <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>

Step two is to setup a class in the same namespace as the EDMX file (easily done by creating class in the same directory as the EDMX file:

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.");
        }
    }
}

Step three is to write an object query pointing to the function:

    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;
    }

The key for me was the CreateQuery call on the object context and syntax of the "query string". Notice the fully qualified reference to the function defined in the EDMX, that was my missing link :-)

like image 121
Bennett Dill Avatar answered Sep 17 '22 16:09

Bennett Dill


That is correct. You cannot create function import for SQL function but only for SQL Stored procedure. You can import SQL function to your storage model but you must manually create method to call the function:

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

Namespace in EdmFunction must be namespace of the store container (SSDL in EDMX file) and name must be the name of imported function. This method makes no sense when called in .NET code. Because of that it throws exception. It is only for queries translated to SQL = linq-to-entities.

like image 28
Ladislav Mrnka Avatar answered Sep 16 '22 16:09

Ladislav Mrnka