How to: Use Object Services with Custom Objects (Entity Framework)

Even if you use custom data classes, you can still take advantage of Object Services functionality provided by data classes generated by Entity Framework tools. This includes the following functionalities:

  • The ability to instantiate an ObjectContext specific to your Entity Data Model (EDM), including predefined connections.

  • Properties that return type-specific ObjectQuery objects.

  • Custom methods that add an object to a specific entity set.

The simplest way to take full advantage of Object Services features is to add the generated class that inherits from ObjectContext to your project. This class is generated by the Entity Framework tools, based on your EDM.

To use generated object code in a C# project

  1. From the command prompt, navigate to the location of the conceptual schema definition language (CSDL) file for your model, and run the following command with the line breaks removed:

    %windir%\Microsoft.NET\Framework\v3.5\edmgen.exe /mode:EntityClassGeneration 
    /incsdl:.\YourModel.csdl /outobjectlayer:.\YourModel.context.cs /language:CSharp
    

    This regenerates the object layer in C#, based on the specified CSDL file.

  2. Open the newly generated code file, copy the class that inherits from ObjectContext, and paste this class into your custom data class code file.

    Note

    You can also remove the data classes and add the remaining class to your project.

To use generated object code in a Visual Basic project

  1. From the command prompt, navigate to the location of the CSDL file for your model, and run the following command with the line breaks removed:

    %windir%\Microsoft.NET\Framework\v3.5\edmgen.exe /mode:EntityClassGeneration 
    /incsdl:.\YourModel.csdl /outobjectlayer:.\YourModel.context.vb /language:VB
    

    This regenerates the object layer in Visual Basic, based on the specified CSDL file.

  2. Open the newly generated code file, copy the class that inherits from ObjectContext, and paste this class into your custom data class code file.

    Note

    You can also remove the data classes and add the remaining class to your project.

Example

This example shows the generated object context code that supports the Order and LineItem custom data classes.

Option Strict Off
Option Explicit On
'''<summary>
'''There are no comments for SalesOrdersEntities in the schema.
'''</summary>
Partial Public Class SalesOrdersEntities
    Inherits Global.System.Data.Objects.ObjectContext
    '''<summary>
    '''Initializes a new SalesOrdersEntities object using the connection string found in the 'SalesOrdersEntities' section of the application configuration file.
    '''</summary>
    Public Sub New()
        MyBase.New("name=SalesOrdersEntities", "SalesOrdersEntities")
    End Sub
    '''<summary>
    '''Initialize a new SalesOrdersEntities object.
    '''</summary>
    Public Sub New(ByVal connectionString As String)
        MyBase.New(connectionString, "SalesOrdersEntities")
    End Sub
    '''<summary>
    '''Initialize a new SalesOrdersEntities object.
    '''</summary>
    Public Sub New(ByVal connection As Global.System.Data.EntityClient.EntityConnection)
        MyBase.New(connection, "SalesOrdersEntities")
    End Sub
    '''<summary>
    '''There are no comments for LineItem in the schema.
    '''</summary>
    <Global.System.ComponentModel.BrowsableAttribute(False)> _
    Public ReadOnly Property LineItemSet() As Global.System.Data.Objects.ObjectQuery(Of LineItem)
        Get
            If (Me._LineItemSet Is Nothing) Then
                Me._LineItemSet = MyBase.CreateQuery(Of LineItem)("[LineItemSet]")
            End If
            Return Me._LineItemSet
        End Get
    End Property
    Private _LineItemSet As Global.System.Data.Objects.ObjectQuery(Of LineItem) = Nothing
    '''<summary>
    '''There are no comments for Order in the schema.
    '''</summary>
    <Global.System.ComponentModel.BrowsableAttribute(False)> _
    Public ReadOnly Property OrderSet() As Global.System.Data.Objects.ObjectQuery(Of Order)
        Get
            If (Me._OrderSet Is Nothing) Then
                Me._OrderSet = MyBase.CreateQuery(Of Order)("[OrderSet]")
            End If
            Return Me._OrderSet
        End Get
    End Property
    Private _OrderSet As Global.System.Data.Objects.ObjectQuery(Of Order) = Nothing
    '''<summary>
    '''There are no comments for LineItem in the schema.
    '''</summary>
    Public Sub AddToLineItem(ByVal lineItem As LineItem)
        MyBase.AddObject("LineItem", lineItem)
    End Sub
    '''<summary>
    '''There are no comments for Order in the schema.
    '''</summary>
    Public Sub AddToOrder(ByVal order As Order)
        MyBase.AddObject("Order", order)
    End Sub
End Class
namespace Microsoft.Samples.Entity
{

    /// <summary>
    /// There are no comments for SalesOrdersEntities in the schema.
    /// </summary>
    public partial class SalesOrdersEntities : global::System.Data.Objects.ObjectContext
    {
        /// <summary>
        /// Initializes a new SalesOrderEntities object using the connection string found in the 'SalesOrderEntities' section of the application configuration file.
        /// </summary>
        public SalesOrdersEntities() :
            base("name=SalesOrdersEntities", "SalesOrdersEntities")
        {
            this.OnContextCreated();
        }
        /// <summary>
        /// Initialize a new SalesOrderEntities object.
        /// </summary>
        public SalesOrdersEntities(string connectionString) :
            base(connectionString, "SalesOrdersEntities")
        {
            this.OnContextCreated();
        }
        /// <summary>
        /// Initialize a new SalesOrderEntities object.
        /// </summary>
        public SalesOrdersEntities(global::System.Data.EntityClient.EntityConnection connection) :
            base(connection, "SalesOrdersEntities")
        {
            this.OnContextCreated();
        }
        partial void OnContextCreated();
        /// <summary>
        /// There are no comments for LineItemSet in the schema.
        /// </summary>
        public global::System.Data.Objects.ObjectQuery<LineItem> LineItemSet
        {
            get
            {
                if ((this._LineItemSet == null))
                {
                    this._LineItemSet = base.CreateQuery<LineItem>("[LineItemSet]");
                }
                return this._LineItemSet;
            }
        }
        private global::System.Data.Objects.ObjectQuery<LineItem> _LineItemSet;
        /// <summary>
        /// There are no comments for OrderSet in the schema.
        /// </summary>
        public global::System.Data.Objects.ObjectQuery<Order> OrderSet
        {
            get
            {
                if ((this._OrderSet == null))
                {
                    this._OrderSet = base.CreateQuery<Order>("[OrderSet]");
                }
                return this._OrderSet;
            }
        }
        private global::System.Data.Objects.ObjectQuery<Order> _OrderSet;
        /// <summary>
        /// There are no comments for LineItemSet in the schema.
        /// </summary>
        public void AddToLineItemSet(LineItem lineItem)
        {
            base.AddObject("LineItemSet", lineItem);
        }
        /// <summary>
        /// There are no comments for OrderSet in the schema.
        /// </summary>
        public void AddToOrderSet(Order order)
        {
            base.AddObject("OrderSet", order);
        }
    }
}

See Also

Tasks

How to: Inherit from the EntityObject and ComplexObject Base Classes (Entity Framework)
How to: Implement Custom Data Class Interfaces (Entity Framework)

Reference

EDM Generator (EdmGen.exe)

Concepts

Customizing Objects (Entity Framework)