none
Scalar Function support in Conceptual Model (EF 6 or EF 6.1) RRS feed

  • Question

  • Hi, looking through old threads, it seems that one cannot use scalar functions in conceptual model. Has this changed with with EF 6 or EF 6.1? How about coming EF 7? Thanks.
    Thursday, June 5, 2014 8:00 AM

Answers

  • Hello,

    For the scalar UDF, unfortunately, it is not supported by default even in Entity Framework 6 or 6.1.

    For Entity Framework 7, since it is not released and there is not related document regarding its new features, we cannot know whether it supports the scalar UDF. You can read more about the plans for EF7 on their blog:

    http://blogs.msdn.com/b/adonet/archive/2014/05/19/ef7-new-platforms-new-data-stores.aspx

    And one way to use it in Entity Framework is to create a stored procedure using your scalar UDF and call the stored procedure in your project.

    Another way is to map it by hand using DbFunctionAttribute to call the function from SSDL directly as below after importing the scalar UDF to the model:

    The class is used to call function from SSDL

    public static class EdmFunctions
    
        {
    
            [DbFunction("DFDBModel.Store", "Function_20140606")]
    
            public static int Function_20140606(int param1, int param2)
    
            {
    
                throw new NotSupportedException("Direct calls are not supported.");
    
            }
    
        }

    The "DFDBModel.Store" is your edmx file namespace rather than your project namepsace, you can open your .edmx file with XML editor and find it.

    Writting an object query pointing to the function:

    List<ObjectParameter> parameters = new List<ObjectParameter>(3);
    
                        parameters.Add(new ObjectParameter("param1", 1));
    
                        parameters.Add(new ObjectParameter("param2", 1));
    
    
                        var output = ((IObjectContextAdapter)db).ObjectContext.CreateQuery<int>("DFDBModel.Store.Function_20140606(@param1, @param2)", parameters.ToArray())
    
                            .Execute(MergeOption.NoTracking)
    
                            .FirstOrDefault();

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Friday, June 6, 2014 2:18 AM
    Moderator