共用方式為


HOW TO:識別本身為 Proxy 的 POCO 實體 (Entity Framework)

本主題顯示如何識別 POCO 實體是否為 Proxy。 有些情況下您會想測試 POCO 物件實際上是否為 Proxy。 當您使用 CreateObject 方法建立 POCO 實體時,如果 POCO 類型不符合建立 POCO Proxy 的需求 (Entity Framework)中所述的需求,將會建立 POCO 實體而非 Proxy 物件。 如需詳細資訊,請參閱使用 POCO 實體 (Entity Framework)

本主題中的範例使用 HOW TO:定義 POCO 實體 (Entity Framework) 中所定義之 POCO 類別,以及 HOW TO:自訂模型與對應檔以搭配自訂物件運作 (Entity Framework) 中所定義之 AdventureWorks 架構資料模型。

範例

下列範例使用 CreateObject 方法來建立 Proxy 物件。 然後,範例會藉由將 POCO 類型與產生的 Proxy 類型相比較,驗證物件是否為 Proxy 物件。 如果類型不相同,它就是一個 Proxy。 如果它是一個 Proxy 物件,至少是一個消極式載入的 Proxy 物件。 若要判斷它是否也是一個變更追蹤 Proxy 物件,我們可以查看是否能追蹤變更。

Public Shared Function IsProxy(ByVal type As Object) As Boolean
    Return type IsNot Nothing AndAlso ObjectContext.GetObjectType(type.GetType()) <> type.GetType()
End Function

Public Shared Sub TestIfEntityIsProxy()
    Using context As New POCOAdventureWorksEntities()
        Dim newItem As LineItem = context.CreateObject(Of LineItem)()
        newItem.SalesOrderDetailID = 0
        ' Assign the order to the new LineItem. 
        newItem.SalesOrderID = 43680
        newItem.OrderQty = 1
        newItem.ProductID = 750
        newItem.UnitPriceDiscount = 0
        newItem.UnitPrice = 2171.2942D
        newItem.ModifiedDate = DateTime.Today
        newItem.rowguid = Guid.NewGuid()
        newItem.SpecialOfferID = 1

        context.LineItems.Attach(newItem)

        ' Determine if the instance is a proxy. 
        ' If it is a proxy it supports lazy loading. 
        Dim isLazyLoading As Boolean = IsProxy(newItem)

        ' Determine if it is a change tracking proxy by 
        ' making a change and verifying that it was detected. 
        newItem.OrderQty = 2
        Dim isChangeTracking As Boolean = _
            context.ObjectStateManager.GetObjectStateEntry(newItem).State = EntityState.Modified
    End Using
End Sub
public static bool IsProxy(object type)
{
    return type != null && ObjectContext.GetObjectType(type.GetType()) != type.GetType();
}

public static void TestIfEntityIsProxy()
{
    using (POCOAdventureWorksEntities context = new POCOAdventureWorksEntities())
    {
        LineItem newItem = context.CreateObject<LineItem>();
        newItem.SalesOrderDetailID = 0;
        // Assign the order to the new LineItem. 
        newItem.SalesOrderID = 43680;
        newItem.OrderQty = 1;
        newItem.ProductID = 750;
        newItem.UnitPriceDiscount = 0;
        newItem.UnitPrice = 2171.2942M;
        newItem.ModifiedDate = DateTime.Today;
        newItem.rowguid = Guid.NewGuid();
        newItem.SpecialOfferID = 1;

        context.LineItems.Attach(newItem);

        // Determine if the instance is a proxy.
        // If it is a proxy it supports lazy loading.
        bool isLazyLoading = IsProxy(newItem);

        // Determine if it is a change tracking proxy by
        // making a change and verifying that it was detected.
        newItem.OrderQty = 2;
        bool isChangeTracking = context.ObjectStateManager
                                  .GetObjectStateEntry(newItem)
                                  .State == EntityState.Modified;
    }
}

另請參閱

工作

HOW TO:建立使用 Proxy 的 POCO 實體 (Entity Framework)

概念

追蹤 POCO 實體中的變更 (Entity Framework)