Helper Methods (EDM)

Entity Data Model (EDM) types defined in schemas do not have associated methods like the classes used in object-oriented programming. Helper methods are user-defined operations that add functionality to entities and associations built by using the EDM.

Helper methods are implemented in partial classes. A partial class splits the definition of a class over two or more source files. Each source file contains a section of the class definition, and all sections are combined when the application is compiled. The helper method described in this example adds a method that is used by types defined in Implementing Associations (EDM). For more information, see Customizing Objects (Entity Framework).

Partial Class Methods

Helper methods that use entities and associations add functionality to applications built on the EDM. The following helper method calculates the ExtendedPrice amount of each of the OrderLines associated with an Order, adds tax, and computes the TotalAmount of the purchase represented by the Order.

The method is implemented in a partial class defined in this example. The following C# source code is compiled together with the code generated from the conceptual schema definition language (CSDL) schema in the EDM project. The result is a method on the Orders class that can be used on instances of the type by application code. For a code segment using this method, see Application Code Using Associations (EDM).

using System;
using System.Data;

namespace OrderInfoModel
{
    public partial class Orders :
                      global::System.Data.Objects.DataClasses.EntityObject
    {
        public decimal ComputeOrder()
        {
            this.TotalAmount = 0;
            foreach (OrderLines orderLine in this.OrderLines)
            {
                orderLine.ExtendedPrice = orderLine.Quantity *
                                           orderLine.UnitPrice;
                this.TotalAmount = this.TotalAmount +
                                       orderLine.ExtendedPrice;
            }
           
            this.Tax = Decimal.Round(((decimal)this.TotalAmount *
                                            (decimal) .08), 2);
            this.TotalAmount = this.TotalAmount + this.Tax;

            return (decimal)this.TotalAmount;

        }
    }
}

See Also

Tasks

How to: Customize Generated Data Objects (Entity Framework)

Concepts

Implementing Entities (EDM)
Implementing Associations (EDM)