Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, dear reader.
I am trying to find out relations between EF Database objects
This is the function to fulfill that task:
C#
private object getCollectionReference(object EFtable, string navigationproperty)
        {
            object collectionreference = new object();
            //now try to query the table
            try
            {
                DbEntityEntry db = ce.Entry(EFtable);
                //query collections of referenced tables
                collectionreference = db
                    .Collection(navigationproperty)
                    .Query()
                    .AsQueryable();
            }
            catch (Exception e)
            {
                //query single referenced table
                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:

     //get the referenced table[s]
object referenceLogAction4 = getCollectionReference(us, "Data_Subjects");
     //get the type
Type ref4 = referenceLogAction4.GetType();
  //load all properties
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);
}
//try loading fields
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
Posted
Updated 25-Nov-14 4:28am
v2

1 solution

Hi, everybody.
I finally found a solution :jig:
object referenceLogAction4 = getCollectionReference(us, "Data_Subjects");

referenceLogAction4 can now be converted using this snippet:
List<Data_Subjects> dsubj = (((System.Data.Entity.Core.Objects.ObjectQuery<Data_Subjects>)referenceLogAction4).ToList());

et voilá, I now can iterate through all returned table references....

kind regards,
Clodetta

PS: How can I mark this question as solved?

PPS: It marked itself as solution...
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900