Hello, dear reader.
I am trying to find out relations between EF Database objects
This is the function to fulfill that task:
private object getCollectionReference(object EFtable, string navigationproperty)
{
object collectionreference = new object();
try
{
DbEntityEntry db = ce.Entry(EFtable);
collectionreference = db
.Collection(navigationproperty)
.Query()
.AsQueryable();
}
catch (Exception e)
{
DbEntityEntry db = ce.Entry(EFtable);
collectionreference = db
.Reference(navigationproperty)
.Query()
.AsQueryable();
}
return collectionreference;
}
That works pretty well until it comes to casting... :-(
The object EFtable can be any Entity in our database.
Is there any opportunity to cast it back into the desired "EF Object" for further processing it?
I read dozens of articles about solving that with Reflections, here is what I´ve done so far:
object referenceLogAction4 = getCollectionReference(us, "Data_Subjects");
Type ref4 = referenceLogAction4.GetType();
IList<PropertyInfo> pops = new List<PropertyInfo>(ref4.GetProperties());
List<object> lobj = new List<object>();
foreach (PropertyInfo pop in pops)
{
object popvalue = pop.GetValue(referenceLogAction4, null);
lobj.Add(popvalue);
}
IList<FieldInfo> fies = new List<FieldInfo>(ref4.GetFields());
List<object> lfie = new List<object>();
foreach (FieldInfo fie in fies)
{
object fiesvalue = fie.GetValue(referenceLogAction4);
lfie.Add(fiesvalue);
}
neither Propertyinfo nor Fieldinfo seems to be what I want; the Fieldinfo part returns zero Fieldinfos, the Propertyinfo values are:
"it"
"Transformed SQL Statement"
[System.Data.Entity.Core.Objects.ObjectContext]
AppendOnly
true
[System.Data.Entity.Core.Objects.ObjectParameterCollection]
true
Seems to me that these are the standard properties of type object...
I also tried casting it via
ToType<T>(this object obj, T type) where T : class
...
but to no avail...
so, to put long things short:
does anybody know, what I need to do to cast the object to my desired EF entity object?
Thanks a lot in advance,
Clodetta