共用方式為


HOW TO:從 EntityObject 和 ComplexObject 基底類別繼承 (Entity Framework)

用 實體資料模型 (EDM) 自訂資料類別時,您必須將自訂資料類別更新為從 EntityObjectComplexObject 繼承。您也必須套用會將自訂類別和屬性 (Property) 對應到概念結構定義語言 (CSDL) 檔案中所定義實體類型和複雜類型的 EDM 屬性 (Attribute)。如需詳細資訊,請參閱 HOW TO:將自訂物件對應到實體 (Entity Framework)

如果不要從 EntityObjectComplexObject 繼承,您也可以實作自訂資料類別介面。如需詳細資訊,請參閱實作自訂資料類別介面 (Entity Framework)

若要從 EntityObject 繼承

  1. 修改每個自訂資料類別的定義,如此它就會繼承自 EntityObject,如下列範例所示:

    <EdmEntityTypeAttribute(NamespaceName:="Microsoft.Samples.Edm", Name:="Order")> _
    Public Class Order
        Inherits EntityObject
    
    [EdmEntityTypeAttribute(NamespaceName="Microsoft.Samples.Edm",Name="Order")]
    public class Order : EntityObject 
    
  2. 在每個資料類別的可設定純量屬性中,在設定屬性值之前,加入 ReportPropertyChanging 的呼叫,然後在設定屬性之後,加入 ReportPropertyChanged 的呼叫。下列範例會顯示這一點:

    Set(ByVal value As Integer)
        ReportPropertyChanging("OrderId")
        _orderId = value
        ReportPropertyChanged("OrderId")
    End Set
    
    set
    {
        ReportPropertyChanging("OrderId");
        _orderId = value;
        ReportPropertyChanged("OrderId");
    }
    

從 ComplexObject 繼承

  1. 修改每一個自訂複雜資料類別的定義,讓它從 ComplexObject 繼承,如以下範例中所示:

    <Global.System.Data.Objects.DataClasses.EdmComplexTypeAttribute( _
        NamespaceName:="Microsoft.Samples.Edm", Name:="OrderInfo")> _
    Partial Public Class OrderInfo
        Inherits Global.System.Data.Objects.DataClasses.ComplexObject
    
    [EdmComplexTypeAttribute(NamespaceName = 
        "Microsoft.Samples.Edm", Name = "OrderInfo")]
    public partial class OrderInfo : ComplexObject
    
  2. 在每個複雜資料類別的可設定純量屬性中,在設定屬性值之前,加入 ReportPropertyChanging 的呼叫,然後在設定屬性之後,加入 ReportPropertyChanged 的呼叫。下列範例會顯示這一點:

    ' Validate the value before setting it.
    If (value <> Nothing) AndAlso value.Length > 25 Then
        Throw New ApplicationException(String.Format( _
                  My.Resources.propertyNotValidString, _
                  "PurchaseOrder", "25"))
    End If
    If _purchaseOrder <> value Then
        ReportPropertyChanging("PurchaseOrder")
        _purchaseOrder = value
        ReportPropertyChanged("PurchaseOrder")
    End If
    
    // Validate the value before setting it.
    if ((value != null) && value.Length > 25)
    {
        throw new ApplicationException(string.Format(
                  Properties.Resources.propertyNotValidString,
                  new string[3] { value, "PurchaseOrder", "25" }));
    }
    if (_purchaseOrder != value)
    {
        ReportPropertyChanging("PurchaseOrder");
        _purchaseOrder = value;
        ReportPropertyChanged("PurchaseOrder");
    }
    

範例

這個範例示範自訂資料類別 OrderLineItem 以及複雜資料類別 OrderInfo。這些自訂類別會對應至 AdventureWorks 資料庫中的 SalesOrderHeaderSalesOrderDetail 資料表。OrderLineItem 類別是繼承自 EntityObject,而複雜 OrderInfo 資料類別則是繼承自 ComplexObject

Option Explicit On
Option Strict On

Imports System
Imports System.Data.SqlTypes
Imports System.Collections.Generic
Imports System.Text
Imports System.Data
Imports System.Data.Objects.DataClasses
Imports System.Data.Metadata.Edm
Imports Microsoft.Samples.Edm

<Assembly: EdmSchemaAttribute()> 
<Assembly: EdmRelationshipAttribute("Microsoft.Samples.Edm", _
    "FK_LineItem_Order_OrderId", "Order", _
    RelationshipMultiplicity.One, GetType(Order), "LineItem", _
    RelationshipMultiplicity.Many, GetType(LineItem))> 
Namespace Microsoft.Samples.Edm

    <EdmEntityTypeAttribute(NamespaceName:="Microsoft.Samples.Edm", Name:="Order")> _
    Public Class Order
        Inherits EntityObject
        ' Define private property variables.
        Private _orderId As Integer
        Private _orderDate As DateTime
        Private _dueDate As DateTime
        Private _shipDate As DateTime
        Private _status As Byte
        Private _customer As Integer
        Private _subTotal As Decimal
        Private _tax As Decimal
        Private _freight As Decimal
        Private _totalDue As Decimal
        Private _extendedInfo As OrderInfo

        'Default Constructor.
        Sub New()

        End Sub
        ' Public properties of the Order object.
        <EdmScalarPropertyAttribute(EntityKeyProperty:=True, IsNullable:=False)> _
        Public Property OrderId() As Integer
            Get
                Return _orderId
            End Get
            Set(ByVal value As Integer)
                ReportPropertyChanging("OrderId")
                _orderId = value
                ReportPropertyChanged("OrderId")
            End Set
        End Property
        ' Navigation property that returns a collection of line items.
        <EdmRelationshipNavigationPropertyAttribute("Microsoft.Samples.Edm", _
                "FK_LineItem_Order_OrderId", "LineItem")> _
        Public ReadOnly Property LineItem() As EntityCollection(Of LineItem)
            Get
                Return CType(Me, IEntityWithRelationships).RelationshipManager _
                    .GetRelatedCollection(Of LineItem) _
                ("FK_LineItem_Order_OrderId", "LineItem")
            End Get
        End Property
        <EdmScalarPropertyAttribute(IsNullable:=False)> _
        Public Property OrderDate() As Date
            Get
                Return _orderDate
            End Get
            Set(ByVal value As DateTime)
                ReportPropertyChanging("OrderDate")
                _orderDate = value
                ReportPropertyChanged("OrderDate")
            End Set
        End Property
        <EdmScalarPropertyAttribute(IsNullable:=False)> _
        Public Property DueDate() As Date
            Get
                Return _dueDate
            End Get
            Set(ByVal value As Date)
                ReportPropertyChanging("DueDate")
                _dueDate = value
                ReportPropertyChanged("DueDate")
            End Set
        End Property
        <EdmScalarPropertyAttribute()> _
        Public Property ShipDate() As Date
            Get
                Return _shipDate
            End Get
            Set(ByVal value As Date)
                ReportPropertyChanging("ShipDate")
                _shipDate = value
                ReportPropertyChanged("ShipDate")
            End Set
        End Property
        <EdmScalarPropertyAttribute(IsNullable:=False)> _
        Public Property Status() As Byte
            Get
                Return _status
            End Get
            Set(ByVal value As Byte)
                If _status <> value Then
                    ReportPropertyChanging("Status")
                    _status = value
                    ReportPropertyChanged("Status")
                End If
            End Set
        End Property
        <EdmScalarPropertyAttribute(IsNullable:=False)> _
        Public Property Customer() As Integer
            Get
                Return _customer
            End Get
            Set(ByVal value As Integer)
                ReportPropertyChanging("Customer")
                _customer = value
                ReportPropertyChanged("Customer")
            End Set
        End Property
        <EdmScalarPropertyAttribute(IsNullable:=False)> _
        Public Property SubTotal() As Decimal
            Get
                Return _subTotal
            End Get
            Set(ByVal value As Decimal)
                If _subTotal <> value Then
                    ' Validate the value before setting it.
                    If value < 0 Then
                        Throw New ApplicationException(String.Format( _
                                  My.Resources.propertyNotValidNegative, _
                                  value.ToString, "SubTotal"))
                    End If
                    ReportPropertyChanging("SubTotal")
                    _subTotal = value
                    ReportPropertyChanged("SubTotal")

                    ' Recalculate the order total.
                    CalculateOrderTotal()
                End If
            End Set
        End Property
        <EdmScalarPropertyAttribute(IsNullable:=False)> _
        Public Property TaxAmt() As Decimal
            Get
                Return _tax
            End Get
            Set(ByVal value As Decimal)
                ' Validate the value before setting it.
                If value < 0 Then
                    Throw New ApplicationException(String.Format( _
                              My.Resources.propertyNotValidNegative, _
                              value.ToString(), "Tax"))
                End If
                ReportPropertyChanging("TaxAmt")
                _tax = value
                ReportPropertyChanged("TaxAmt")

                ' Recalculate the order total.
                CalculateOrderTotal()
            End Set
        End Property
        <EdmScalarPropertyAttribute(IsNullable:=False)> _
        Public Property Freight() As Decimal
            Get
                Return _freight
            End Get
            Set(ByVal value As Decimal)
                If _freight <> value Then
                    ' Validate the value before setting it.
                    If value < 0 Then
                        Throw New ApplicationException(String.Format( _
                                  My.Resources.propertyNotValidNegative, _
                        value.ToString(), "Freight"))
                    End If
                    ReportPropertyChanging("Freight")
                    _freight = value
                    ReportPropertyChanging("Freight")

                    ' Recalculate the order total.
                    CalculateOrderTotal()
                End If
            End Set
        End Property
        Public ReadOnly Property TotalDue() As Decimal
            Get
                Return _totalDue
            End Get
        End Property
        <EdmComplexPropertyAttribute()> _
        Public Property ExtendedInfo() As OrderInfo
            Get
                Return _extendedInfo
            End Get
            Set(ByVal value As OrderInfo)
                ReportPropertyChanging("ExtendedInfo")
                _extendedInfo = value
                ReportPropertyChanged("ExtendedInfo")
            End Set
        End Property
        Private Sub CalculateOrderTotal()
            ' Update the total due as a sum of the other cost properties.
            _totalDue = _subTotal + _tax + _freight
        End Sub
    End Class
    <Global.System.Data.Objects.DataClasses.EdmComplexTypeAttribute( _
        NamespaceName:="Microsoft.Samples.Edm", Name:="OrderInfo")> _
    Partial Public Class OrderInfo
        Inherits Global.System.Data.Objects.DataClasses.ComplexObject
        Private _orderNumber As String
        Private _purchaseOrder As String
        Private _accountNumber As String
        Private _comment As String
        <EdmScalarPropertyAttribute(IsNullable:=False)> _
    Public Property OrderNumber() As String
            Get
                Return _orderNumber
            End Get
            Set(ByVal value As String)
                ' Validate the value before setting it.
                If value.Length > 25 Then
                    Throw New ApplicationException(String.Format( _
                        My.Resources.propertyNotValidString, _
                        "OrderNumber", "25"))
                End If
                ReportPropertyChanging("OrderNumber")
                _orderNumber = value
                ReportPropertyChanged("OrderNumber")
            End Set
        End Property
        <EdmScalarPropertyAttribute()> _
        Public Property PurchaseOrder() As String
            Get
                Return _purchaseOrder
            End Get
            Set(ByVal value As String)
                ' Validate the value before setting it.
                If (value <> Nothing) AndAlso value.Length > 25 Then
                    Throw New ApplicationException(String.Format( _
                              My.Resources.propertyNotValidString, _
                              "PurchaseOrder", "25"))
                End If
                If _purchaseOrder <> value Then
                    ReportPropertyChanging("PurchaseOrder")
                    _purchaseOrder = value
                    ReportPropertyChanged("PurchaseOrder")
                End If
            End Set
        End Property
        <EdmScalarPropertyAttribute()> _
        Public Property AccountNumber() As String
            Get
                Return _accountNumber
            End Get
            Set(ByVal value As String)
                ' Validate the value before setting it.
                If (value <> Nothing) AndAlso value.Length > 15 Then
                    Throw New ApplicationException(String.Format( _
                              My.Resources.propertyNotValidString, _
                              "AccountNumber", "15"))
                End If
                ReportPropertyChanging("AccountNumber")
                _accountNumber = value
                ReportPropertyChanged("AccountNumber")
            End Set
        End Property
        <EdmScalarPropertyAttribute()> _
    Public Property Comment() As String
            Get
                Return _comment
            End Get
            Set(ByVal value As String)
                ' Validate the value before setting it.
                If (value <> Nothing) AndAlso value.Length > 128 Then
                    Throw New ApplicationException(String.Format( _
                              My.Resources.propertyNotValidString, _
                              "Comment", "128"))
                End If
                If _comment <> value Then
                    ReportPropertyChanging("Comment")
                    _comment = value
                    ReportPropertyChanged("Comment")
                End If
            End Set
        End Property
    End Class
    <EdmEntityTypeAttribute(NamespaceName:="Microsoft.Samples.Edm", _
                            Name:="LineItem")> _
    Public Class LineItem
        Inherits EntityObject

        ' Define private property variables.
        Dim _lineItemId As Integer
        Dim _trackingNumber As String
        Dim _quantity As Short
        Dim _product As Integer
        Dim _price As Decimal
        Dim _discount As Decimal
        Dim _total As Decimal
        Sub New()

        End Sub
        ' Defines a navigation property to the Order class.
        <EdmRelationshipNavigationPropertyAttribute("Microsoft.Samples.Edm", _
                "FK_LineItem_Order_OrderId", "Order")> _
        Public Property Order() As Order
            Get
                Return CType(Me,  _
                IEntityWithRelationships).RelationshipManager _
                    .GetRelatedReference(Of Order) _
                    ("FK_LineItem_Order_OrderId", "Order").Value
            End Get
            Set(ByVal value As Order)
                CType(Me,  _
                IEntityWithRelationships).RelationshipManager _
                    .GetRelatedReference(Of Order) _
                    ("FK_LineItem_Order_OrderId", "Order").Value = value
            End Set
        End Property
        <EdmScalarPropertyAttribute(EntityKeyProperty:=True, IsNullable:=False)> _
        Public Property LineItemId() As Integer
            Get
                Return _lineItemId
            End Get
            Set(ByVal value As Integer)
                ReportPropertyChanging("LineItemId")
                _lineItemId = value
                ReportPropertyChanged("LineItemId")
            End Set
        End Property
        <EdmScalarPropertyAttribute()> _
        Public Property TrackingNumber() As String
            Get
                Return _trackingNumber
            End Get
            Set(ByVal value As String)
                If _trackingNumber <> value Then
                    ' Validate the value before setting it.
                    If value.Length > 25 Then
                        Throw New ApplicationException(String.Format( _
                                My.Resources.propertyNotValidString, _
                                "TrackingNumber", "25"))
                    End If
                    ReportPropertyChanging("TrackingNumber")
                    _trackingNumber = value
                    ReportPropertyChanged("TrackingNumber")
                End If
            End Set
        End Property
        <EdmScalarPropertyAttribute(IsNullable:=False)> _
        Public Property Quantity() As Short
            Get
                Return _quantity
            End Get
            Set(ByVal value As Short)
                If _quantity <> value Then
                    ' Validate the value before setting it.
                    If value < 1 Then
                        Throw New ApplicationException(String.Format( _
                            My.Resources.propertyNotValidNegative, _
                            value.ToString(), "Quantity"))
                    End If
                    ReportPropertyChanging("Quantity")
                    _quantity = value
                    ReportPropertyChanged("Quantity")

                    ' Update the line total.
                    CalculateLineTotal()
                End If
            End Set
        End Property
        <EdmScalarPropertyAttribute(IsNullable:=False)> _
        Public Property Product() As Integer
            Get
                Return _product
            End Get
            Set(ByVal value As Integer)
                ' Validate the value before setting it.
                If value < 1 Then
                    Throw New ApplicationException(String.Format( _
                              My.Resources.propertyNotValidNegative, _
                              value.ToString(), "Product"))
                End If
                ReportPropertyChanging("Product")
                _product = value
                ReportPropertyChanged("Product")
            End Set
        End Property
        <EdmScalarPropertyAttribute(IsNullable:=False)> _
        Public Property Price() As Decimal
            Get
                Return _price
            End Get
            Set(ByVal value As Decimal)
                If _price <> value Then
                    ' Validate the value before setting it.
                    If value < 0 Then
                        Throw New ApplicationException(String.Format( _
                                  My.Resources.propertyNotValidNegative, _
                                  value.ToString(), "Price"))
                    End If
                    ReportPropertyChanging("Price")
                    _price = value
                    ReportPropertyChanged("Price")

                    ' Update the line total.
                    CalculateLineTotal()
                End If
            End Set
        End Property
        <EdmScalarPropertyAttribute(IsNullable:=False)> _
        Public Property Discount() As Decimal
            Get
                Return _discount
            End Get
            Set(ByVal value As Decimal)
                ' Validate the value before setting it.
                If value < 0 Then
                    Throw New ApplicationException(String.Format( _
                              My.Resources.propertyNotValidNegative, _
                              value.ToString(), "Discount"))
                End If
                ReportPropertyChanging("Discount")
                _discount = value
                ReportPropertyChanged("Discount")
            End Set
        End Property
        Public ReadOnly Property Total() As Decimal
            Get
                Return _total
            End Get
        End Property
        Private Sub CalculateLineTotal()
            _total = (_quantity * (_price - _discount))
        End Sub
    End Class
End Namespace
using System;
using System.Data.SqlTypes;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Objects.DataClasses;
using System.Data.Metadata.Edm;
using Microsoft.Samples.Edm;

[assembly: EdmSchemaAttribute()]
[assembly: EdmRelationshipAttribute("Microsoft.Samples.Edm",
    "FK_LineItem_Order_OrderId", "Order", 
    RelationshipMultiplicity.One, typeof(Order),"LineItem",
    RelationshipMultiplicity.Many, typeof(LineItem))]
namespace Microsoft.Samples.Edm
{   
    [EdmEntityTypeAttribute(NamespaceName="Microsoft.Samples.Edm",Name="Order")]
    public class Order : EntityObject 
    {
        // Define private property variables.
        private int _orderId;
        private DateTime _orderDate;
        private DateTime _dueDate;
        private DateTime _shipDate;
        private byte _status;
        private int _customer;
        private decimal _subTotal;
        private decimal _tax;
        private decimal _freight;
        private decimal _totalDue;
        private OrderInfo _ExtendedInfo;
        
        // Public properties of the Order object.
        [EdmScalarPropertyAttribute(EntityKeyProperty = true, IsNullable = false)]
        public int OrderId
        {
            get 
            {
                return _orderId;
            }
            set
            {
                ReportPropertyChanging("OrderId");
                _orderId = value;
                ReportPropertyChanged("OrderId");
            }
        }

        // Navigation property that returns a collection of line items.
        [EdmRelationshipNavigationPropertyAttribute("Microsoft.Samples.Edm","FK_LineItem_Order_OrderId", "LineItem")]
        public System.Data.Objects.DataClasses.EntityCollection<LineItem> LineItem
        {
            get
            {
                return ((IEntityWithRelationships)(this)).RelationshipManager.
                    GetRelatedCollection<LineItem>("FK_LineItem_Order_OrderId", "LineItem");
            }
        }
        [EdmScalarPropertyAttribute(IsNullable = false)]
        public DateTime OrderDate 
        {
           get 
            {
                return _orderDate;
            }
            set
            {
                ReportPropertyChanging("OrderDate");
                _orderDate = value;
                ReportPropertyChanged("OrderDate");
            }
        }
        [EdmScalarPropertyAttribute(IsNullable = false)]
        public DateTime DueDate 
        {
            get 
            {
                return _dueDate;
            }
            set
            {
                ReportPropertyChanging("DueDate");
                _dueDate = value;
                ReportPropertyChanged("DueDate");
            }
        }
        [EdmScalarPropertyAttribute()]
        public DateTime ShipDate
        {
            get
            {
                return _shipDate;
            }
            set
            {
                ReportPropertyChanging("ShipDate");
                _shipDate = value;
                ReportPropertyChanged("ShipDate");

            }
        }
        [EdmScalarPropertyAttribute(IsNullable = false)]
        public byte Status
        {
            get 
            {
                return _status;
            }
            set
            {
                if (_status != value)
                {
                    ReportPropertyChanging("Status");
                    _status = value;
                    ReportPropertyChanged("Status");
                }
            }
        }
        [EdmScalarPropertyAttribute(IsNullable = false)]
        public int Customer
        {
            get
            {
                return _customer;
            }
            set
            {
                ReportPropertyChanging("Customer");
                _customer = value;
                ReportPropertyChanged("Customer");
            }
        }
        [EdmScalarPropertyAttribute(IsNullable = false)]
        public decimal SubTotal
        {
            get
            {
                return _subTotal;
            }
            set 
            {
                if (_subTotal != value)
                {
                    // Validate the value before setting it.
                    if (value < 0)
                    {
                        throw new ApplicationException(string.Format(
                                  Properties.Resources.propertyNotValidNegative,
                                  new string[2] { value.ToString(), "SubTotal" }));
                    }

                    ReportPropertyChanging("SubTotal");
                    _subTotal = value;
                    ReportPropertyChanged("SubTotal");

                    // Recalculate the order total.
                    CalculateOrderTotal();
                }
            }
        }
        [EdmScalarPropertyAttribute(IsNullable = false)]
        public decimal TaxAmt
        {
            get
            {
                return _tax;
            }
            set
            {
                // Validate the value before setting it.
                if (value < 0)
                {
                    throw new ApplicationException(string.Format(
                              Properties.Resources.propertyNotValidNegative,
                              new string[2] { value.ToString(), "Tax" }));
                }

                ReportPropertyChanging("TaxAmt");
                _tax = value;
                ReportPropertyChanged("TaxAmt");

                // Recalculate the order total.
                CalculateOrderTotal();
            }
        }
        [EdmScalarPropertyAttribute(IsNullable = false)]
        public decimal Freight
        {
            get
            {
                return _freight;
            }
            set
            {
                if (_freight != value)
                {
                    // Validate the value before setting it.
                    if (value < 0)
                    {
                        throw new ApplicationException(string.Format(
                                  Properties.Resources.propertyNotValidNegative,
                                  new string[2] { value.ToString(), "Freight" }));
                    }

                    ReportPropertyChanging("Freight");
                    _freight = value;
                    ReportPropertyChanging("Freight");

                    // Recalculate the order total.
                    CalculateOrderTotal();
                }
            }

        }
        public decimal TotalDue
        {
            get
            {
                return _totalDue;
            }
        }

        [EdmComplexPropertyAttribute()]
        public OrderInfo ExtendedInfo
        {
            get
            {
                return _ExtendedInfo;
            }
            set
            {
                this.ReportPropertyChanging("ExtendedInfo");
                _ExtendedInfo = value;
                this.ReportPropertyChanged("ExtendedInfo");                
            }
        }
        private void CalculateOrderTotal()
        {
            // Update the total due as a sum of the other cost properties.
            _totalDue = _subTotal + _tax + _freight;
        }
}
    [EdmComplexTypeAttribute(NamespaceName = 
        "Microsoft.Samples.Edm", Name = "OrderInfo")]
    public partial class OrderInfo : ComplexObject
    {
        private string _orderNumber;
        private string _purchaseOrder;
        private string _accountNumber;
        private string _comment;

        [EdmScalarPropertyAttribute(IsNullable = false)]
        public string OrderNumber
        {
            get
            {
                return _orderNumber;
            }
            set
            {
                // Validate the value before setting it.
                if (value.Length > 25)
                {
                    throw new ApplicationException(string.Format(
                              Properties.Resources.propertyNotValidString,
                              new string[3] { value, "OrderNumber", "25" }));
                }

                ReportPropertyChanging("OrderNumber");
                _orderNumber = value;
                ReportPropertyChanged("OrderNumber");
            }
        }
        [EdmScalarPropertyAttribute()]
        public string PurchaseOrder
        {
            get
            {
                return _purchaseOrder;
            }
            set
            {
                // Validate the value before setting it.
                if ((value != null) && value.Length > 25)
                {
                    throw new ApplicationException(string.Format(
                              Properties.Resources.propertyNotValidString,
                              new string[3] { value, "PurchaseOrder", "25" }));
                }
                if (_purchaseOrder != value)
                {
                    ReportPropertyChanging("PurchaseOrder");
                    _purchaseOrder = value;
                    ReportPropertyChanged("PurchaseOrder");
                }
            }
        }
        [EdmScalarPropertyAttribute()]
        public string AccountNumber
        {
            get
            {
                return _accountNumber;
            }
            set
            {
                // Validate the value before setting it.
                if ((value != null) && value.Length > 15)
                {
                    throw new ApplicationException(string.Format(
                              Properties.Resources.propertyNotValidString,
                              new string[3] { value, "AccountNumber", "15" }));
                }
                ReportPropertyChanging("AccountNumber");
                _accountNumber = value;
                ReportPropertyChanged("AccountNumber");
            }
        }
        [EdmScalarPropertyAttribute()]
        public string Comment
        {
            get
            {
                return _comment;
            }
            set
            {
                // Validate the value before setting it.
                if ((value != null) && value.Length > 128)
                {
                    throw new ApplicationException(string.Format(
                              Properties.Resources.propertyNotValidString,
                              new string[3] { value, "Comment", "128" }));
                }
                if (_comment != value)
                {
                    ReportPropertyChanging("Comment");
                    _comment = value;
                    ReportPropertyChanged("Comment");
                }
            }
        }
    }

    [EdmEntityTypeAttribute(NamespaceName = "Microsoft.Samples.Edm", Name = "LineItem")]
    public class LineItem : EntityObject  
    {
        // Define private property variables.
        int _lineItemId; 
        string _trackingNumber;
        short _quantity;
        int _product;
        decimal _price;
        decimal _discount;
        decimal _total;

        // Default constructor.
        public LineItem()
        {

        }   

        // Defines a navigation property to the Order class.
        [EdmRelationshipNavigationPropertyAttribute("Microsoft.Samples.Edm", "FK_LineItem_Order_OrderId", "Order")]
        public Order Order
        {
            get
            {
                return ((IEntityWithRelationships)(this)).RelationshipManager.
                    GetRelatedReference<Order>("FK_LineItem_Order_OrderId", "Order").Value;
            }
            set
            {
                ((IEntityWithRelationships)(this)).RelationshipManager.
                    GetRelatedReference<Order>("FK_LineItem_Order_OrderId", "Order").Value = value;
            }
        }
        [EdmScalarPropertyAttribute(EntityKeyProperty = true, IsNullable = false)]
        public int LineItemId
        {
            get
            {
                return _lineItemId;
            }
            set
            {
                ReportPropertyChanging("LineItemId");
                _lineItemId = value;
                ReportPropertyChanged("LineItemId");
            }
        }
        [EdmScalarPropertyAttribute()]
        public string TrackingNumber
        {
            get
            {
                return _trackingNumber;
            }
            set
            {
                if (_trackingNumber != value)
                {
                    // Validate the value before setting it.
                    if (value.Length > 25)
                    {
                        throw new ApplicationException(string.Format(
                                  Properties.Resources.propertyNotValidString,
                                  new string[3] {value,"TrackingNumber", "25"}));
                    }
                    ReportPropertyChanging("TrackingNumber");
                    _trackingNumber = value;
                    ReportPropertyChanged("TrackingNumber");
                }
            }
        }
        [EdmScalarPropertyAttribute(IsNullable = false)]
        public short Quantity
        {
            get
            {
                return _quantity;
            }
            set
            {
                if (_quantity != value)
                {
                    // Validate the value before setting it.
                    if (value < 1)
                    {
                        throw new ApplicationException(string.Format(
                                  Properties.Resources.propertyNotValidNegative,
                                  new string[2] { value.ToString(), "Quantity" }));
                    }
                    ReportPropertyChanging("Quantity");
                    _quantity = value;
                    ReportPropertyChanged("Quantity");
 
                    // Update the line total.
                    CalculateLineTotal();
                }
            }
        }
        [EdmScalarPropertyAttribute(IsNullable = false)]
        public int Product
        {
            get
            {
                return _product;
            }
            set
            {
                // Validate the value before setting it.
                if (value < 1)
                {
                    throw new ApplicationException(string.Format(
                              Properties.Resources.propertyNotValidNegative, 
                              new string[2] { value.ToString(), "Product" }));
                }
                ReportPropertyChanging("Product");
                _product = value;
                ReportPropertyChanged("Product");
            }
        }
        [EdmScalarPropertyAttribute(IsNullable = false)] 
        public decimal Price
        {
            get
            {
                return _price;
            }
            set
            {
                if (_price != value)
                {
                    // Validate the value before setting it.
                    if (value < 0)
                    {
                        throw new ApplicationException(string.Format(
                                  Properties.Resources.propertyNotValidNegative,
                                  new string[2] { value.ToString(), "Price" }));
                    }

                    ReportPropertyChanging("Price");
                    _price = value;
                    ReportPropertyChanged("Price");

                    // Update the line total.
                    CalculateLineTotal();
                }
            }
        }
        [EdmScalarPropertyAttribute(IsNullable = false)]
        public decimal Discount
        {
            get
            {
                return _discount;
            }
            set
            {
                // Validate the value before setting it.
                if (value < 0)
                {
                    throw new ApplicationException(string.Format(
                              Properties.Resources.propertyNotValidNegative,
                              new string[2] { value.ToString(), "Discount" }));
                }
                ReportPropertyChanging("Discount");
                _discount = value;
                ReportPropertyChanged("Discount");
            }
        }
 
        public decimal Total
        {
            get
            {
                return _total;
            }
        }

        private void CalculateLineTotal()
        {
            _total = (_quantity * (_price - _discount)); 
        }
    }
}

另請參閱

工作

HOW TO:自訂 Entity Data Model 以搭配自訂物件運作 (Entity Framework)
HOW TO:實作自訂資料類別介面 (Entity Framework)

概念

自訂物件 (Entity Framework)

其他資源

處理自訂物件 (Entity Framework 工作)