Partilhar via


Serializable POCO Classes Based on the AdventureWorks Model

This example contains definitions for serializable POCO entity classes and a custom object context class. The POCO entity classes are based on the AdventureWorks data model that is defined in How to: Customize Modeling and Mapping Files to Work with Custom Objects.

In the code page for your application, add the following using statements (Imports in Visual Basic):

using System.Data.Objects;
using System.Runtime.Serialization;
#region Contexts

public partial class POCOAdventureWorksEntities : ObjectContext
{
    private ObjectSet<Contact> _contacts;
    private ObjectSet<LineItem> _lineItems;
    private ObjectSet<Order> _orders;
    #region Constructors
    public POCOAdventureWorksEntities()
        : base("name=AdventureWorksEntities", "AdventureWorksEntities")
    {
    }

    public POCOAdventureWorksEntities(string connectionString)
        : base(connectionString, "AdventureWorksEntities")
    {
    }
    #endregion

    #region ObjectSet Properties
    public ObjectSet<Contact> Contacts
    {
        get
        {
            return _contacts ?? (_contacts = base.CreateObjectSet<Contact>());
        }
    }

    public ObjectSet<LineItem> LineItems
    {
        get
        {
            return _lineItems ?? (_lineItems = base.CreateObjectSet<LineItem>());
        }
    }

    public ObjectSet<Order> Orders
    {
        get
        {
            return _orders ?? (_orders = base.CreateObjectSet<Order>());
        }
    }
    #endregion
}
#endregion


#region Entities
[DataContract(IsReference = true)]
public class LineItem
{
    [DataMember]
    public virtual Int32 SalesOrderID { get; set; }
    [DataMember]
    public virtual Int32 SalesOrderDetailID { get; set; }
    [DataMember]
    public virtual String CarrierTrackingNumber { get; set; }
    [DataMember]
    public virtual Int16 OrderQty { get; set; }
    [DataMember]
    public virtual Int32 ProductID { get; set; }
    [DataMember]
    public virtual Int32 SpecialOfferID { get; set; }
    [DataMember]
    public virtual Decimal UnitPrice { get; set; }
    [DataMember]
    public virtual Decimal UnitPriceDiscount { get; set; }
    [DataMember]
    public virtual Decimal LineTotal { get; set; }
    [DataMember]
    public virtual Guid rowguid { get; set; }
    [DataMember]
    public virtual DateTime ModifiedDate { get; set; }
    [DataMember]
    // Defines a navigation property to the Order object.
    public virtual Order Order { get; set; }
}

[DataContract(IsReference = true)]
public class Order
{
    [DataMember]
    public virtual Int32 SalesOrderID { get; set; }
    [DataMember]
    public virtual Byte RevisionNumber { get; set; }
    [DataMember]
    public virtual DateTime OrderDate { get; set; }
    [DataMember]
    public virtual DateTime DueDate { get; set; }
    [DataMember]
    public virtual Nullable<DateTime> ShipDate { get; set; }
    [DataMember]
    public virtual Byte Status { get; set; }
    [DataMember]
    public virtual Boolean OnlineOrderFlag { get; set; }
    [DataMember]
    public virtual String SalesOrderNumber { get; set; }
    [DataMember]
    public virtual String PurchaseOrderNumber { get; set; }
    [DataMember]
    public virtual String AccountNumber { get; set; }
    [DataMember]
    public virtual Int32 CustomerID { get; set; }
    [DataMember]
    public virtual Int32 ContactID { get; set; }
    [DataMember]
    public virtual Nullable<Int32> SalesPersonID { get; set; }
    [DataMember]
    public virtual Nullable<Int32> TerritoryID { get; set; }
    [DataMember]
    public virtual Int32 BillToAddressID { get; set; }
    [DataMember]
    public virtual Int32 ShipToAddressID { get; set; }
    [DataMember]
    public virtual Int32 ShipMethodID { get; set; }
    [DataMember]
    public virtual Nullable<Int32> CreditCardID { get; set; }
    [DataMember]
    public virtual String CreditCardApprovalCode { get; set; }
    [DataMember]
    public virtual Nullable<Int32> CurrencyRateID { get; set; }
    [DataMember]
    public virtual Decimal SubTotal { get; set; }
    [DataMember]
    public virtual Decimal TaxAmt { get; set; }
    [DataMember]
    public virtual Decimal Freight { get; set; }
    [DataMember]
    public virtual Decimal TotalDue { get; set; }
    [DataMember]
    public virtual String Comment { get; set; }
    [DataMember]
    public virtual Guid rowguid { get; set; }
    [DataMember]
    public virtual DateTime ModifiedDate { get; set; }
    [DataMember]
    public virtual OrderInfo ExtendedInfo { get; set; }
    [DataMember]
    // Defines a navigation property to the collection of LineItem objects.
    public virtual ICollection<LineItem> LineItems { get; set; }
    [DataMember]
    // Defines a navigation property to the Contact object.
    public virtual Contact Contact { get; set; }
}

[DataContract(IsReference = true)]
public class OrderInfo
{
    [DataMember]
    public string SalesOrderNumber { get; set; }
    [DataMember]
    public string PurchaseOrderNumber { get; set; }
    [DataMember]
    public string AccountNumber { get; set; }
    [DataMember]
    public string Comment { get; set; }
}

[DataContract(IsReference = true)]
public class Contact
{
    [DataMember]
    public virtual Int32 ContactID { get; set; }
    [DataMember]
    public virtual Boolean NameStyle { get; set; }
    [DataMember]
    public virtual String Title { get; set; }
    [DataMember]
    public virtual String FirstName { get; set; }
    [DataMember]
    public virtual String MiddleName { get; set; }
    [DataMember]
    public virtual String LastName { get; set; }
    [DataMember]
    public virtual String Suffix { get; set; }
    [DataMember]
    public virtual String EmailAddress { get; set; }
    [DataMember]
    public virtual Int32 EmailPromotion { get; set; }
    [DataMember]
    public virtual String Phone { get; set; }
    [DataMember]
    public virtual String PasswordHash { get; set; }
    [DataMember]
    public virtual String PasswordSalt { get; set; }
    [DataMember]
    public virtual String AdditionalContactInfo { get; set; }
    [DataMember]
    public virtual Guid rowguid { get; set; }
    [DataMember]
    public virtual DateTime ModifiedDate { get; set; }
    [DataMember]
    // Defines a navigation property to the collection of Order objects.
    public virtual ICollection<Order> Orders { get; set; }
}
#endregion

See Also

Concepts

Working with POCO Entities