簡體   English   中英

如何使用反射獲取斷開連接對象的已加載EF4.0屬性的列表

[英]How to get a list of loaded EF4.0 properties of a disconnected object using reflection

如何使用反射獲取斷開連接的EF對象的已加載屬性列表?

我可以用

var targetProperty.GetValue(targetObject, null);
var isLoaded = ((System.Data.Objects.DataClasses.RelatedEnd)(propertyValue)).IsLoaded;

但這僅適用於集合屬性。 對於單個對象屬性,效果不佳。 它將給消息

ObjectContext實例已被處置,不能再用於需要連接的操作。

我使用的代碼類似於以下代碼

IEnumerable<object> q;

using(var context = new InfosContext())
{
  q = context.Person.Include("Father").Take(10).ToList();
}
foreach(var value in q)
{
  var sourceProperties = value.GetType().GetProperties().ToList();
  var singleProperties = sourceProperties.Where(i => i.PropertyType.AssemblyQualifiedName.StartsWith("TestProject.Models.Entities")).ToList();
  foreach(var i in singleProperties)
  {
     var propertyValue = i.GetValue(value, null);  // BOOM.  Exception here.
     if (propertyValue != null && ((System.Data.Objects.DataClasses.RelatedEnd)(propertyValue)).IsLoaded) 
     {
     }
  }
}

任何幫助將不勝感激。

foreach(var i in singleProperties)
{
  var reference = value.GetType().GetProperty(i.Name + "Reference");
  var refValue = reference.GetValue(value, null);
  var isLoaded = ((System.Data.Objects.DataClasses.EntityReference)(refValue)).IsLoaded;
  if(isLoaded)
  {
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM