Model Defined Function

A model-defined function is a function that is defined in a conceptual model of EDMX. The body of the MDF (Model Define Function) is representing in Entity SQL, which allows the function to be used independently of rules or languages supported in the data source.
 
The ADO.NET Entity Framework uses the domain-specific language (DSL) called conceptual schema definition language (CSDL) to define conceptual models in EDMX. All MDF must be defined in the CSDL.
 
The following information is required to define MDF.
  • Function Name (custom function name to identify specific function).
  • Parameter (The parameter that passes for retrieving specific information).
  • Return Type (optional parameter. If there is no return type then define it to void).
  • Body of the function.
Define MDF in the Conceptual Model
 
To define MDF, add a "Function" element with a "DefiningExpression" element to the conceptual Model of an EDMX. In MDF, we may define any Entity SQL statement, including parameterized statements.
 
The following Example explains how can we add a function in the conceptual model and call it from LINQ - entity query and ESQL.
  1. <edmx:ConceptualModels>  
  2.      <Schema Namespace="AdventureWorksModel" Alias="Self" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns="http://schemas.microsoft.com/ado/2008/09/edm">  
  3.           <EntityContainer Name="Entities" annotation:LazyLoadingEnabled="true">  
  4.                <EntitySet Name="Departments" EntityType="AdventureWorksModel.Department" />  
  5.                <EntitySet Name="Employees" EntityType="AdventureWorksModel.Employee" />  
  6.                <AssociationSet Name="FK_Employees_Department" Association="AdventureWorksModel.FK_Employees_Department">  
  7.                     <End Role="Departments" EntitySet="Departments" />  
  8.                     <End Role="Employees" EntitySet="Employees" />  
  9.                </AssociationSet>  
  10.           </EntityContainer>  
  11.           <Function Name="GetDepartmentCode" ReturnType="Edm.String">  
  12.                <Parameter Name="DepartmentId" Type="Edm.Int32"></Parameter>  
  13.                <DefiningExpression>  
  14.                     ANYELEMENT(SELECT VALUE SC.DepartmentCode  
  15.                     FROM Entities.Departments AS SC  
  16.                     WHERE SC.DepartmentId = DepartmentId)  
  17.                </DefiningExpression>  
  18.           </Function>  
  19.           <EntityType Name="Department">  
  20.                <Key>  
  21.                     <PropertyRef Name="DepartmentId" />  
  22.                </Key>  
  23.                <Property Name="DepartmentId" Type="Int32" Nullable="false" />  
  24.                <Property Name="DepartmentCode" Type="String" Nullable="false" MaxLength="25" Unicode="false" FixedLength="false" />  
  25.                <Property Name="DepartmentName" Type="String" Nullable="false" MaxLength="100" Unicode="false" FixedLength="false" />  
  26.                <Property Name="IsActive" Type="Boolean" Nullable="false" />  
  27.                <Property Name="DepartmentType" Type="Int32" Nullable="false" />  
  28.                <NavigationProperty Name="Employees" Relationship="AdventureWorksModel.FK_Employees_Department" FromRole="Departments" ToRole="Employees" />  
  29.           </EntityType>  
  30.           <EntityType Name="Employee">  
  31.                <Key>  
  32.                     <PropertyRef Name="EmployeeId" />  
  33.                </Key>  
  34.                <Property Name="EmployeeId" Type="Int32" Nullable="false" />  
  35.                <Property Name="EmployeeCode" Type="String" Nullable="false" MaxLength="25" Unicode="false" FixedLength="false" />  
  36.                <Property Name="Name" Type="String" Nullable="false" MaxLength="150" Unicode="false" FixedLength="false" />  
  37.                <Property Name="DepartmentId" Type="Int32" Nullable="false" />  
  38.                <Property Name="Isactive" Type="Boolean" Nullable="false" />  
  39.                <NavigationProperty Name="Department" Relationship="AdventureWorksModel.FK_Employees_Department" FromRole="Employees" ToRole="Departments" />  
  40.           </EntityType>  
  41.           <Association Name="FK_Employees_Department">  
  42.                <End Role="Departments" Type="AdventureWorksModel.Department" Multiplicity="1" />  
  43.                <End Role="Employees" Type="AdventureWorksModel.Employee" Multiplicity="*" />  
  44.                <ReferentialConstraint>  
  45.                     <Principal Role="Departments">  
  46.                          <PropertyRef Name="DepartmentId" />  
  47.                     </Principal>  
  48.                     <Dependent Role="Employees">  
  49.                          <PropertyRef Name="DepartmentId" />  
  50.                     </Dependent>  
  51.                </ReferentialConstraint>  
  52.           </Association>  
  53.      </Schema>  
  54. </edmx:ConceptualModels> 
If we want to use this function LINQ to Entity, we must define a CLR function.
  1. public static class mdfClass {  
  2.   [EdmFunction("AdventureWorksModel""GetDepartmentCode")]  
  3.   public static string GetDepartmentCode(int DepartmentId) {  
  4.     throw new NotSupportedException();  
  5.   }  
Call Function from LINQ to Entity.
  1. Entities e = new Entities();  
  2. var f = from g in e.Employees  
  3. select new Details {  
  4.   EmpCode = g.EmployeeCode,  
  5.     Name = g.Name,  
  6.     DeptCode = mdfClass.GetDepartmentCode(g.DepartmentId)  
  7. }; 
Call Function from ESQL.
  1. var qry1 = "select AdventureWorksModel.GetDepartmentCode(E.DepartmentId) as DeptCode,E.EmployeeCode as EmpCode,E.Name as Name FROM Entities.Employees AS E";  
  2. ObjectQuery < DbDataRecord > query1 = new ObjectQuery < DbDataRecord > (qry1, e);  
  3.   
  4. foreach(DbDataRecord i1 in query1) {  
  5.   Console.WriteLine(i1.GetName(0) + " : " + i1.GetValue(0));  
  6.   Console.WriteLine(i1.GetName(1) + " : " + i1.GetValue(1));  
  7.   Console.WriteLine(i1.GetName(2) + " : " + i1.GetValue(2));  
  8.   
Mdl.gif


Similar Articles