Condividi tramite


Procedura: definire entità POCO (Entity Framework)

Entity Framework consente di utilizzare delle proprie classi di dati personalizzate. Per ulteriori informazioni, vedere Utilizzo di entità POCO (Entity Framework). In questo argomento viene definito un set di tipi POCO mappate in modo implicito, avendo gli stessi tipi e nomi di proprietà, al modello di dati definito in Procedura: personalizzare i file di mapping e di modellazione per l'utilizzo con oggetti personalizzati (Entity Framework). Questo modello è basato sulle tabelle Contact, SalesOrderHeader e SalesOrderDetail del database AdventureWorks. Per informazioni sulla definizione di una classe del contesto dell'oggetto personalizzata, vedere Procedura: definire un contesto dell'oggetto personalizzato (Entity Framework).

Esempio

In questo esempio vengono definite le classi di dati personalizzate Contact, Order, LineItem e OrderInfo. Queste classi sono state definite per supportare la creazione di oggetti proxy. Per ulteriori informazioni, vedere Requisiti per la creazione di proxy POCO (Entity Framework).

Public Class LineItem
    Private _SalesOrderID As Int32
    Public Overridable Property SalesOrderID() As Int32
        Get
            Return _SalesOrderID
        End Get
        Set(ByVal value As Int32)
            _SalesOrderID = value
        End Set
    End Property
    Private _SalesOrderDetailID As Int32
    Public Overridable Property SalesOrderDetailID() As Int32
        Get
            Return _SalesOrderDetailID
        End Get
        Set(ByVal value As Int32)
            _SalesOrderDetailID = value
        End Set
    End Property
    Private _CarrierTrackingNumber As String
    Public Overridable Property CarrierTrackingNumber() As String
        Get
            Return _CarrierTrackingNumber
        End Get
        Set(ByVal value As String)
            _CarrierTrackingNumber = value
        End Set
    End Property
    Private _OrderQty As Int16
    Public Overridable Property OrderQty() As Int16
        Get
            Return _OrderQty
        End Get
        Set(ByVal value As Int16)
            _OrderQty = value
        End Set
    End Property
    Private _ProductID As Int32
    Public Overridable Property ProductID() As Int32
        Get
            Return _ProductID
        End Get
        Set(ByVal value As Int32)
            _ProductID = value
        End Set
    End Property
    Private _SpecialOfferID As Int32
    Public Overridable Property SpecialOfferID() As Int32
        Get
            Return _SpecialOfferID
        End Get
        Set(ByVal value As Int32)
            _SpecialOfferID = value
        End Set
    End Property
    Private _UnitPrice As Decimal
    Public Overridable Property UnitPrice() As Decimal
        Get
            Return _UnitPrice
        End Get
        Set(ByVal value As Decimal)
            _UnitPrice = value
        End Set
    End Property
    Private _UnitPriceDiscount As Decimal
    Public Overridable Property UnitPriceDiscount() As Decimal
        Get
            Return _UnitPriceDiscount
        End Get
        Set(ByVal value As Decimal)
            _UnitPriceDiscount = value
        End Set
    End Property
    Private _LineTotal As Decimal
    Public Overridable Property LineTotal() As Decimal
        Get
            Return _LineTotal
        End Get
        Set(ByVal value As Decimal)
            _LineTotal = value
        End Set
    End Property
    Private _rowguid As Guid
    Public Overridable Property rowguid() As Guid
        Get
            Return _rowguid
        End Get
        Set(ByVal value As Guid)
            _rowguid = value
        End Set
    End Property
    Private _ModifiedDate As DateTime
    Public Overridable Property ModifiedDate() As DateTime
        Get
            Return _ModifiedDate
        End Get
        Set(ByVal value As DateTime)
            _ModifiedDate = value
        End Set
    End Property
    ' Defines a navigation property to the Order object. 
    Private _Order As Order
    Public Overridable Property Order() As Order
        Get
            Return _Order
        End Get
        Set(ByVal value As Order)
            _Order = value
        End Set
    End Property
End Class

Public Class Order
    Private _SalesOrderID As Int32
    Public Overridable Property SalesOrderID() As Int32
        Get
            Return _SalesOrderID
        End Get
        Set(ByVal value As Int32)
            _SalesOrderID = value
        End Set
    End Property
    Private _RevisionNumber As Byte
    Public Overridable Property RevisionNumber() As Byte
        Get
            Return _RevisionNumber
        End Get
        Set(ByVal value As Byte)
            _RevisionNumber = value
        End Set
    End Property
    Private _OrderDate As DateTime
    Public Overridable Property OrderDate() As DateTime
        Get
            Return _OrderDate
        End Get
        Set(ByVal value As DateTime)
            _OrderDate = value
        End Set
    End Property
    Private _DueDate As DateTime
    Public Overridable Property DueDate() As DateTime
        Get
            Return _DueDate
        End Get
        Set(ByVal value As DateTime)
            _DueDate = value
        End Set
    End Property
    Private _ShipDate As Nullable(Of DateTime)
    Public Overridable Property ShipDate() As Nullable(Of DateTime)
        Get
            Return _ShipDate
        End Get
        Set(ByVal value As Nullable(Of DateTime))
            _ShipDate = value
        End Set
    End Property
    Private _Status As Byte
    Public Overridable Property Status() As Byte
        Get
            Return _Status
        End Get
        Set(ByVal value As Byte)
            _Status = value
        End Set
    End Property
    Private _OnlineOrderFlag As Boolean
    Public Overridable Property OnlineOrderFlag() As Boolean
        Get
            Return _OnlineOrderFlag
        End Get
        Set(ByVal value As Boolean)
            _OnlineOrderFlag = value
        End Set
    End Property
    Private _SalesOrderNumber As String
    Public Overridable Property SalesOrderNumber() As String
        Get
            Return _SalesOrderNumber
        End Get
        Set(ByVal value As String)
            _SalesOrderNumber = value
        End Set
    End Property
    Private _PurchaseOrderNumber As String
    Public Overridable Property PurchaseOrderNumber() As String
        Get
            Return _PurchaseOrderNumber
        End Get
        Set(ByVal value As String)
            _PurchaseOrderNumber = value
        End Set
    End Property
    Private _AccountNumber As String
    Public Overridable Property AccountNumber() As String
        Get
            Return _AccountNumber
        End Get
        Set(ByVal value As String)
            _AccountNumber = value
        End Set
    End Property
    Private _CustomerID As Int32
    Public Overridable Property CustomerID() As Int32
        Get
            Return _CustomerID
        End Get
        Set(ByVal value As Int32)
            _CustomerID = value
        End Set
    End Property
    Private _ContactID As Int32
    Public Overridable Property ContactID() As Int32
        Get
            Return _ContactID
        End Get
        Set(ByVal value As Int32)
            _ContactID = value
        End Set
    End Property
    Private _SalesPersonID As Nullable(Of Int32)
    Public Overridable Property SalesPersonID() As Nullable(Of Int32)
        Get
            Return _SalesPersonID
        End Get
        Set(ByVal value As Nullable(Of Int32))
            _SalesPersonID = value
        End Set
    End Property
    Private _TerritoryID As Nullable(Of Int32)
    Public Overridable Property TerritoryID() As Nullable(Of Int32)
        Get
            Return _TerritoryID
        End Get
        Set(ByVal value As Nullable(Of Int32))
            _TerritoryID = value
        End Set
    End Property
    Private _BillToAddressID As Int32
    Public Overridable Property BillToAddressID() As Int32
        Get
            Return _BillToAddressID
        End Get
        Set(ByVal value As Int32)
            _BillToAddressID = value
        End Set
    End Property
    Private _ShipToAddressID As Int32
    Public Overridable Property ShipToAddressID() As Int32
        Get
            Return _ShipToAddressID
        End Get
        Set(ByVal value As Int32)
            _ShipToAddressID = value
        End Set
    End Property
    Private _ShipMethodID As Int32
    Public Overridable Property ShipMethodID() As Int32
        Get
            Return _ShipMethodID
        End Get
        Set(ByVal value As Int32)
            _ShipMethodID = value
        End Set
    End Property
    Private _CreditCardID As Nullable(Of Int32)
    Public Overridable Property CreditCardID() As Nullable(Of Int32)
        Get
            Return _CreditCardID
        End Get
        Set(ByVal value As Nullable(Of Int32))
            _CreditCardID = value
        End Set
    End Property
    Private _CreditCardApprovalCode As String
    Public Overridable Property CreditCardApprovalCode() As String
        Get
            Return _CreditCardApprovalCode
        End Get
        Set(ByVal value As String)
            _CreditCardApprovalCode = value
        End Set
    End Property
    Private _CurrencyRateID As Nullable(Of Int32)
    Public Overridable Property CurrencyRateID() As Nullable(Of Int32)
        Get
            Return _CurrencyRateID
        End Get
        Set(ByVal value As Nullable(Of Int32))
            _CurrencyRateID = value
        End Set
    End Property
    Private _SubTotal As Decimal
    Public Overridable Property SubTotal() As Decimal
        Get
            Return _SubTotal
        End Get
        Set(ByVal value As Decimal)
            _SubTotal = value
        End Set
    End Property
    Private _TaxAmt As Decimal
    Public Overridable Property TaxAmt() As Decimal
        Get
            Return _TaxAmt
        End Get
        Set(ByVal value As Decimal)
            _TaxAmt = value
        End Set
    End Property
    Private _Freight As Decimal
    Public Overridable Property Freight() As Decimal
        Get
            Return _Freight
        End Get
        Set(ByVal value As Decimal)
            _Freight = value
        End Set
    End Property
    Private _TotalDue As Decimal
    Public Overridable Property TotalDue() As Decimal
        Get
            Return _TotalDue
        End Get
        Set(ByVal value As Decimal)
            _TotalDue = value
        End Set
    End Property
    Private _Comment As String
    Public Overridable Property Comment() As String
        Get
            Return _Comment
        End Get
        Set(ByVal value As String)
            _Comment = value
        End Set
    End Property
    Private _rowguid As Guid
    Public Overridable Property rowguid() As Guid
        Get
            Return _rowguid
        End Get
        Set(ByVal value As Guid)
            _rowguid = value
        End Set
    End Property
    Private _ModifiedDate As DateTime
    Public Overridable Property ModifiedDate() As DateTime
        Get
            Return _ModifiedDate
        End Get
        Set(ByVal value As DateTime)
            _ModifiedDate = value
        End Set
    End Property
    Private _ExtendedInfo As OrderInfo
    Public Overridable Property ExtendedInfo() As OrderInfo
        Get
            Return _ExtendedInfo
        End Get
        Set(ByVal value As OrderInfo)
            _ExtendedInfo = value
        End Set
    End Property
    ' Defines a navigation property to the collection of LineItem objects. 
    Private _LineItems As ICollection(Of LineItem)
    Public Overridable Property LineItems() As ICollection(Of LineItem)
        Get
            Return _LineItems
        End Get
        Set(ByVal value As ICollection(Of LineItem))
            _LineItems = value
        End Set
    End Property
    ' Defines a navigation property to the Contact object. 
    Private _Contact As Contact
    Public Overridable Property Contact() As Contact
        Get
            Return _Contact
        End Get
        Set(ByVal value As Contact)
            _Contact = value
        End Set
    End Property
End Class


Public Class OrderInfo
    Private _SalesOrderNumber As String
    Public Property SalesOrderNumber() As String
        Get
            Return _SalesOrderNumber
        End Get
        Set(ByVal value As String)
            _SalesOrderNumber = value
        End Set
    End Property
    Private _PurchaseOrderNumber As String
    Public Property PurchaseOrderNumber() As String
        Get
            Return _PurchaseOrderNumber
        End Get
        Set(ByVal value As String)
            _PurchaseOrderNumber = value
        End Set
    End Property
    Private _AccountNumber As String
    Public Property AccountNumber() As String
        Get
            Return _AccountNumber
        End Get
        Set(ByVal value As String)
            _AccountNumber = value
        End Set
    End Property
    Private _Comment As String
    Public Property Comment() As String
        Get
            Return _Comment
        End Get
        Set(ByVal value As String)
            _Comment = value
        End Set
    End Property
End Class


Public Class Contact
    Private _ContactID As Int32
    Public Overridable Property ContactID() As Int32
        Get
            Return _ContactID
        End Get
        Set(ByVal value As Int32)
            _ContactID = value
        End Set
    End Property
    Private _NameStyle As Boolean
    Public Overridable Property NameStyle() As Boolean
        Get
            Return _NameStyle
        End Get
        Set(ByVal value As Boolean)
            _NameStyle = value
        End Set
    End Property
    Private _Title As String
    Public Overridable Property Title() As String
        Get
            Return _Title
        End Get
        Set(ByVal value As String)
            _Title = value
        End Set
    End Property
    Private _FirstName As String
    Public Overridable Property FirstName() As String
        Get
            Return _FirstName
        End Get
        Set(ByVal value As String)
            _FirstName = value
        End Set
    End Property
    Private _MiddleName As String
    Public Overridable Property MiddleName() As String
        Get
            Return _MiddleName
        End Get
        Set(ByVal value As String)
            _MiddleName = value
        End Set
    End Property
    Private _LastName As String
    Public Overridable Property LastName() As String
        Get
            Return _LastName
        End Get
        Set(ByVal value As String)
            _LastName = value
        End Set
    End Property
    Private _Suffix As String
    Public Overridable Property Suffix() As String
        Get
            Return _Suffix
        End Get
        Set(ByVal value As String)
            _Suffix = value
        End Set
    End Property
    Private _EmailAddress As String
    Public Overridable Property EmailAddress() As String
        Get
            Return _EmailAddress
        End Get
        Set(ByVal value As String)
            _EmailAddress = value
        End Set
    End Property
    Private _EmailPromotion As Int32
    Public Overridable Property EmailPromotion() As Int32
        Get
            Return _EmailPromotion
        End Get
        Set(ByVal value As Int32)
            _EmailPromotion = value
        End Set
    End Property
    Private _Phone As String
    Public Overridable Property Phone() As String
        Get
            Return _Phone
        End Get
        Set(ByVal value As String)
            _Phone = value
        End Set
    End Property
    Private _PasswordHash As String
    Public Overridable Property PasswordHash() As String
        Get
            Return _PasswordHash
        End Get
        Set(ByVal value As String)
            _PasswordHash = value
        End Set
    End Property
    Private _PasswordSalt As String
    Public Overridable Property PasswordSalt() As String
        Get
            Return _PasswordSalt
        End Get
        Set(ByVal value As String)
            _PasswordSalt = value
        End Set
    End Property
    Private _AdditionalContactInfo As String
    Public Overridable Property AdditionalContactInfo() As String
        Get
            Return _AdditionalContactInfo
        End Get
        Set(ByVal value As String)
            _AdditionalContactInfo = value
        End Set
    End Property
    Private _rowguid As Guid
    Public Overridable Property rowguid() As Guid
        Get
            Return _rowguid
        End Get
        Set(ByVal value As Guid)
            _rowguid = value
        End Set
    End Property
    Private _ModifiedDate As DateTime
    Public Overridable Property ModifiedDate() As DateTime
        Get
            Return _ModifiedDate
        End Get
        Set(ByVal value As DateTime)
            _ModifiedDate = value
        End Set
    End Property
    ' Defines a navigation property to the collection of Order objects. 
    Private _Orders As ICollection(Of Order)
    Public Overridable Property Orders() As ICollection(Of Order)
        Get
            Return _Orders
        End Get
        Set(ByVal value As ICollection(Of Order))
            _Orders = value
        End Set
    End Property
End Class
public class LineItem
{
    public virtual Int32 SalesOrderID { get; set; }
    public virtual Int32 SalesOrderDetailID { get; set; }
    public virtual String CarrierTrackingNumber { get; set; }
    public virtual Int16 OrderQty { get; set; }
    public virtual Int32 ProductID { get; set; }
    public virtual Int32 SpecialOfferID { get; set; }
    public virtual Decimal UnitPrice { get; set; }
    public virtual Decimal UnitPriceDiscount { get; set; }
    public virtual Decimal LineTotal { get; set; }
    public virtual Guid rowguid { get; set; }
    public virtual DateTime ModifiedDate { get; set; }
    // Defines a navigation property to the Order object.
    public virtual Order Order { get; set; }
}

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

public class OrderInfo
{
    public string SalesOrderNumber { get; set; }
    public string PurchaseOrderNumber { get; set; }
    public string AccountNumber { get; set; }
    public string Comment { get; set; }
}

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

Vedere anche

Concetti

Personalizzazione di oggetti (Entity Framework)