19

I'm using Entity Framework 4. I am using a database first model, meaning that I generated the EDM from the database. Now I want to add some model-defined functions. My question is ... where?

If I put them in the .edmx file, won't all my additions be clobbered the next time I update the database and generate the new EDM? I mean it says it right there at the top of the .Designer.cs file, "Manual changes to this file will be overwritten if the code is regenerated."

So, in what file do I put my additions?

Rap
  • 6,851
  • 3
  • 50
  • 88

1 Answers1

36

I will take it little bit deeply because model defined functions are not very well known.

Model defined functions must be manually added to CSDL part of EDMX file. You must open file as XML and add a function. For example this model defined function is able to produce full name of the employee:

<Function Name="FullName" ReturnType="Edm.String">
  <Parameter Name="emp" Type="TestModel.Employee" />
  <DefiningExpression>
    Trim(emp.FirstName) + " " + Trim(emp.LastName)
  </DefiningExpression>
</Function>

Now you can save your EDMX and return to designer. The function will be still present but it is not visible in Model browser. You can update your model from database or delete all your entities but the function will be still defined. EF doesn't remove custom modification in CSDL part of EDMX.

Now you need to define the .NET function to be able to use this model defined function. You can do it anywhere. One way is to use partial class to context but in the same time you can just use some custom class:

public static class EdmFunctions
{
    [EdmFunction("TestModel", "FullName")]
    public static string FullName(Employee e)
    {
        throw new NotSupportedException("This function is only for L2E query.");
    }
}

And you are done. The only remaining task is using the function in Linq-to-Entities query:

using (var context = new TestEntities())
{
    var query = from e in context.Employees
                select new
                    {
                        e.Id,
                        FullName = EdmFunctions.FullName(e)
                    };
    var data = query.ToList();
    ...
}

Model defined functions are just some reusable Entity SQL which is translated to SQL so they can be only used in Linq-to-Entities queries. Model defined functions can be much more complicated.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • 3
    Great answer - good to know model defined functions are not destroyed when you update the model – BrokenGlass Apr 10 '11 at 18:04
  • 2
    As a follow-up, there is an MSDN article on this as well: [How to: Define Custom Functions in the Conceptual Model (Entity Framework)](http://msdn.microsoft.com/en-us/library/dd456812.aspx) – Brad Christie May 16 '12 at 19:10
  • Joel Mueller's answer to another question is similar to what you've mentioned - it's very thorough and also shows how to make this work for LINQ-to-Objects as well: [LINQ to Entities does not recognize the method 'Double Parse(System.String)' method, and this method cannot be translated into a store expression][1] [1]: http://stackoverflow.com/a/5971677/35133 – David McClelland May 21 '13 at 21:50
  • `[EDMFunction]` is now obsolete, use `[DbFunction]` instead. – General Grievance Sep 03 '21 at 15:01