Export (0) Print
Expand All

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.

    NoteNote

    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.

    NoteNote

    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.

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

Community Additions

Show:
© 2014 Microsoft