如何:确定某个 POCO 实体是代理(实体框架)

本主题演示如何确定某个 POCO 实体是否为代理。 有时您可能要测试 POCO 对象是否实际上是代理。 当您使用 CreateObject 方法创建 POCO 实体时,如果 POCO 类型不符合创建 POCO 代理的要求(实体框架)中所述的要求,则会创建一个 POCO 实体,而不是代理对象。 有关更多信息,请参见使用 POCO 实体(实体框架)

本主题中的示例使用在如何:定义 POCO 实体(实体框架)中定义的 POCO 类和在如何:自定义建模和映射文件以使用自定义对象(实体框架)中定义的基于 AdventureWorks 的数据模型。

示例

下面的示例使用 CreateObject 方法创建一个代理对象。 然后,该示例通过将 POCO 类型与生成的代理类型进行比较来验证该对象是否为代理对象。 如果这两个类型不同,则该对象为代理对象。 如果该对象是代理对象,则它至少是一个延迟加载代理对象。 若要确定它是否还是更改跟踪代理对象,我们可以查看更改是否得到跟踪。

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;
    }
}

另请参见

任务

如何:创建带有代理的 POCO 实体(实体框架)

概念

跟踪 POCO 实体中的更改(实体框架)