共用方式為


以 AdventureWorks Model 為基礎的可序列化 POCO 類別

此範例包含可序列化之 POCO 實體類別和自訂物件內容類別的定義。 POCO 實體類別是以 HOW TO:自訂模型與對應檔以搭配自訂物件運作 (Entity Framework) 中所定義的 AdventureWorks 資料模型為基礎。

在應用程式的字碼頁中加入下列 using 陳述式 (在 Visual Basic 中為 Imports):

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

另請參閱

概念

使用 POCO 實體 (Entity Framework)