Nota
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare ad accedere o modificare le directory.
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare a modificare le directory.
La modalità consigliata per utilizzare le classi di dati personalizzate con Entity Data Model (EDM) consiste nell'ereditare da EntityObject e da ComplexObject. Per i casi in cui non è possibile ereditare da EntityObject e da ComplexObject oppure quando si necessita di una maggiore indipendenza dal framework, Entity Framework fornisce un set di interfacce di classi di dati personalizzate. Se non si eredita da EntityObject, per utilizzare le classi di dati personalizzate con EDM è necessario implementare queste interfacce. Le interfacce specifiche implementate dipendono dai requisiti delle classi di dati personalizzate e dell'applicazione.
IEntityWithChangeTracker
Necessaria per il rilevamento delle modifiche. Consente a Object Services di rilevare le modifiche all'oggetto.Object Services fornisce agli oggetti l'interfaccia IEntityChangeTracker per consentire la segnalazione delle modifiche. IEntityWithChangeTracker definisce il metodo SetChangeTracker. Questo metodo specifica l'oggetto IEntityChangeTracker utilizzato per segnalare le modifiche. Per ulteriori informazioni, vedere Segnalazione delle modifiche nelle classi di dati personalizzate (Entity Framework).
IEntityWithKey
Facoltativa. Espone la chiave di entità a Object Services per consentire un miglioramento delle prestazioni.IEntityWithKey definisce la proprietà EntityKey. La proprietà EntityKey viene utilizzata da Object Services per gestire gli oggetti nel contesto dell'oggetto.
Se si sceglie di non implementare IEntityWithKey, si verificheranno una riduzione delle prestazioni e un aumento della memoria utilizzata quando si caricano gli oggetti correlati, si connettono gli oggetti a un contesto dell'oggetto o si esegue qualsiasi operazione che richiede una chiave.
IEntityWithRelationships
Obbligatoria per le entità con associazioni. Consente a Object Services di gestire le relazioni tra gli oggetti.IEntityWithRelationships definisce la proprietà RelationshipManager. La proprietà RelationshipManager viene utilizzata da Object Services per accedere all'oggetto RelationshipManager utilizzato per gestire le relazioni con gli altri oggetti.
Per ulteriori informazioni, vedere Procedura: implementare interfacce di classi di dati personalizzate (Entity Framework).
Come le classi di dati personalizzate che ereditano da EntityObject, le classi che implementano queste interfacce devono soddisfare i requisiti seguenti:
Deve essere disponibile un oggetto per ogni tipo di entità definito nel file CSDL (Conceptual Schema Definition Language).
Allo spazio dei nomi, alle classi e alle proprietà dei dati devono essere applicati gli attributi EDM appropriati.
I nomi dello spazio dei nomi, delle classi e delle proprietà dei dati a cui sono applicati gli attributi EDM devono corrispondere ai nomi nel file CSDL corrispondente.
Per ulteriori informazioni, vedere Personalizzazione di oggetti (Entity Framework).
Nell'esempio seguente viene illustrato il codice richiesto per implementare queste interfacce per un oggetto Order, dove Order è basato sulla tabella SalesOrderHeader.
Dim _changeTracker As IEntityChangeTracker = Nothing
' Specify the IEntityChangeTracker to use for tracking changes.
Private Sub SetChangeTracker(ByVal changeTracker As IEntityChangeTracker) _
Implements IEntityWithChangeTracker.SetChangeTracker
_changeTracker = changeTracker
' Every time the change tracker is set, we must also set all the
' complex type change trackers.
If Not _extendedInfo Is Nothing Then
_extendedInfo.SetComplexChangeTracker("ExtendedInfo", _changeTracker)
End If
End Sub
Dim _entityKey As EntityKey = Nothing
' Define the EntityKey property for the class.
Property EntityKey() As EntityKey Implements IEntityWithKey.EntityKey
Get
Return _entityKey
End Get
Set(ByVal value As EntityKey)
' Set the EntityKey property, if it is not set.
' Report the change if the change tracker exists.
If Not _changeTracker Is Nothing Then
_changeTracker.EntityMemberChanging(StructuralObject.EntityKeyPropertyName)
_entityKey = value
_changeTracker.EntityMemberChanged(StructuralObject.EntityKeyPropertyName)
Else
_entityKey = value
End If
End Set
End Property
Dim _relationships As RelationshipManager = Nothing
' Define a relationship manager for the class.
ReadOnly Property RelationshipManager() As RelationshipManager _
Implements IEntityWithRelationships.RelationshipManager
Get
If _relationships Is Nothing Then
_relationships = RelationshipManager.Create(Me)
End If
Return _relationships
End Get
End Property
IEntityChangeTracker _changeTracker = null;
// Specify the IEntityChangeTracker to use for tracking changes.
void IEntityWithChangeTracker.SetChangeTracker(IEntityChangeTracker changeTracker)
{
_changeTracker = changeTracker;
// Every time the change tracker is set, we must also set all the
// complex type change trackers.
if (_extendedInfo != null)
{
_extendedInfo.SetComplexChangeTracker("ExtendedInfo", _changeTracker);
}
}
EntityKey _entityKey = null;
// Define the EntityKey property for the class.
EntityKey IEntityWithKey.EntityKey
{
get
{
return _entityKey;
}
set
{
// Set the EntityKey property, if it is not set.
// Report the change if the change tracker exists.
if (_changeTracker != null)
{
_changeTracker.EntityMemberChanging(StructuralObject.EntityKeyPropertyName);
_entityKey = value;
_changeTracker.EntityMemberChanged(StructuralObject.EntityKeyPropertyName);
}
else
{
_entityKey = value;
}
}
}
RelationshipManager _relationships = null;
// Define a relationship manager for the class.
RelationshipManager IEntityWithRelationships.RelationshipManager
{
get
{
if (null == _relationships)
_relationships = RelationshipManager.Create(this);
return _relationships;
}
}
Tipi complessi
I tipi complessi sono proprietà non scalari di tipi di entità che consentono l'organizzazione delle proprietà scalari nelle entità. Per ulteriori informazioni, vedere Tipo complesso (EDM). Il rilevamento delle modifiche negli oggetti di tipo complesso richiede la scrittura di codice di rilevamento delle modifiche personalizzato. È pertanto consigliabile ereditare da EntityObject e da ComplexObject, quando possibile. Non sono disponibili interfacce di classi di dati personalizzate da implementare per gli oggetti di tipo complesso. È tuttavia possibile utilizzare la procedura descritta di seguito per implementare il rilevamento delle modifiche con gli oggetti di tipo complesso che non ereditano da ComplexObject.
Nota |
|---|
Se si sceglie di implementare le interfacce di classi di dati personalizzate per gli oggetti, ma anche di ereditare da ComplexObject, è ancora necessario implementare il rilevamento delle modifiche personalizzato come descritto nella procedura seguente. |
Per implementare il rilevamento delle modifiche per gli oggetti di tipo complesso
Implementare le interfacce di dati personalizzate per i tipi di entità. Per ulteriori informazioni, vedere Procedura: implementare interfacce di classi di dati personalizzate (Entity Framework).
Verificare che i tipi complessi siano definiti correttamente nella sezione concettuale e in quella di mapping di EDM. Per ulteriori informazioni, vedere Tipo complesso (EDM).
Definire una classe di base astratta denominata ComplexTypeChangeTracker.
' Base class for complex types that implements change tracking. Public MustInherit Class ComplexTypeChangeTracker Protected _complexChangeTracker As IEntityChangeTracker = Nothing Private _rootComplexPropertyName As String ' Gets an IEntityChangeTracker to call for properties change. ' You must do this in order to track changes. Public Overridable Sub SetComplexChangeTracker( _ ByVal rootComplexPropertyName As String, _ ByVal complexChangeTracker As IEntityChangeTracker) _rootComplexPropertyName = rootComplexPropertyName _complexChangeTracker = complexChangeTracker End Sub ' Protected method that is called before the change for change tracking ' each of the scalar properties in the complex type. Protected Sub ReportMemberChanging(ByVal scalarPropertyName As String) If Not _complexChangeTracker Is Nothing Then _complexChangeTracker.EntityComplexMemberChanging( _ _rootComplexPropertyName, Me, scalarPropertyName) End If End Sub ' Protected method that is called after the change for change tracking ' each of the scalar properties in the complex type. Protected Sub ReportMemberChanged(ByVal scalarPropertyName As String) If Not _complexChangeTracker Is Nothing Then _complexChangeTracker.EntityComplexMemberChanged( _ _rootComplexPropertyName, Me, scalarPropertyName) End If End Sub End Class// Base class for complex types that implements change tracking. public abstract class ComplexTypeChangeTracker { protected IEntityChangeTracker _complexChangeTracker = null; private string _rootComplexPropertyName; // Gets an IEntityChangeTracker to call for properties change. // You must do this in order to track changes. virtual public void SetComplexChangeTracker(string rootComplexPropertyName, IEntityChangeTracker complexChangeTracker) { _rootComplexPropertyName = rootComplexPropertyName; _complexChangeTracker = complexChangeTracker; } // Protected method that is called before the change for change tracking // each of the scalar properties in the complex type. protected void ReportMemberChanging(string scalarPropertyName) { if (null != _complexChangeTracker) { _complexChangeTracker.EntityComplexMemberChanging(_rootComplexPropertyName, this, scalarPropertyName); } } // Protected method that is called after the change for change tracking // each of the scalar properties in the complex type. protected void ReportMemberChanged(string scalarPropertyName) { if (null != _complexChangeTracker) { _complexChangeTracker.EntityComplexMemberChanged(_rootComplexPropertyName, this, scalarPropertyName); } } }Definire la classe del tipo complesso che eredita da ComplexTypeChangeTracker e applicare EdmComplexTypeAttribute.
<EdmComplexTypeAttribute(NamespaceName:="Microsoft.Samples.Edm", Name:="OrderInfo")> _ Partial Public Class OrderInfo Inherits ComplexTypeChangeTracker[EdmComplexTypeAttribute(NamespaceName = "Microsoft.Samples.Edm", Name = "OrderInfo")] public partial class OrderInfo : ComplexTypeChangeTracker {Nella classe del tipo complesso eseguire l'override del metodo SetComplexChangeTracker.
Public Overrides Sub SetComplexChangeTracker(ByVal rootComplexPropertyName As String, _ ByVal changeTracker As IEntityChangeTracker) ' Call SetChangeTracker on the base class to set the change tracker ' and the name of the root complex type property on the entity. MyBase.SetComplexChangeTracker(rootComplexPropertyName, changeTracker) End Suboverride public void SetComplexChangeTracker(string rootComplexPropertyName, IEntityChangeTracker changeTracker) { // Call SetChangeTracker on the base class to set the change tracker // and the name of the root complex type property on the entity. base.SetComplexChangeTracker(rootComplexPropertyName, changeTracker); }Implementare il rilevamento delle modifiche standard nelle proprietà scalari del tipo complesso. Per ulteriori informazioni, vedere Segnalazione delle modifiche nelle classi di dati personalizzate (Entity Framework).
Applicare EdmComplexPropertyAttribute alla proprietà complessa nel tipo di entità e aggiungere una chiamata a SetComplexChangeTracker per reimpostare il rilevamento delle modifiche quando la proprietà complessa viene modificata.
<EdmComplexPropertyAttribute()> _ Public Property ExtendedInfo() As OrderInfo Get Return _extendedInfo End Get Set(ByVal value As OrderInfo) ' For a complex type any changes in the complex type ' properties all get tracked together. ' The change tracker may be Nothing during object materialization. If Not _changeTracker Is Nothing Then ' Since this is a complex property, we need to reset the change ' tracker on the complex type. If Not _extendedInfo Is Nothing Then ' Reset the change tracker. _extendedInfo.SetComplexChangeTracker("ExtendedInfo", Nothing) End If ' Report the change. _changeTracker.EntityMemberChanging("ExtendedInfo") _extendedInfo = value _changeTracker.EntityMemberChanging("ExtendedInfo") Else _extendedInfo = value End If ' Rest the change tracker. Complex type property cannot be Nothing. If Not _extendedInfo Is Nothing Then _extendedInfo.SetComplexChangeTracker("ExtendedInfo", _changeTracker) End If End Set End Property[EdmComplexPropertyAttribute()] public OrderInfo ExtendedInfo { get { return _extendedInfo; } set { // For a complex type any changes in the complex type // properties all get tracked together. // The change tracker may be null during object materialization. if (_changeTracker != null) { // Since this is a complex property, we need to reset the change // tracker on the complex type. if (_extendedInfo != null) { // Reset the change tracker. _extendedInfo.SetComplexChangeTracker("ExtendedInfo", null); } // Report the change. _changeTracker.EntityMemberChanging("ExtendedInfo"); _extendedInfo = value; _changeTracker.EntityMemberChanged("ExtendedInfo"); } else { _extendedInfo = value; } // Reset the change tracker. Complex type property cannot be null. if (_extendedInfo != null) { _extendedInfo.SetComplexChangeTracker("ExtendedInfo", _changeTracker); } } }Ripetere i passaggi da 4 a 7 per ogni proprietà complessa.
Nell'implementazione System.Data.Objects.DataClasses.IEntityWithChangeTracker.SetChangeTracker(System.Data.Objects.DataClasses.IEntityChangeTracker) per il tipo di entità, inserire una chiamata a SetComplexChangeTracker per impostare il rilevamento delle modifiche. Eseguire questa operazione una volta per ogni proprietà complessa nel tipo.
' Every time the change tracker is set, we must also set all the ' complex type change trackers. If Not _extendedInfo Is Nothing Then _extendedInfo.SetComplexChangeTracker("ExtendedInfo", _changeTracker) End If// Every time the change tracker is set, we must also set all the // complex type change trackers. if (_extendedInfo != null) { _extendedInfo.SetComplexChangeTracker("ExtendedInfo", _changeTracker); }
Nota