samedi 11 novembre 2017

System.Data.Entity.Core.Objects.ObjectQuery' does not contain a definition for 'EntitySet'

I'm just sharing this because it drove me crazy for a while, So, posting the question and answer just in case someone runs into the same situation.

I was using reflection in one of my projects to get a value from a generic type. The problem isn't with the generic Type method invocation, but rather that the actual return value is itself a generic type.

Since I don't know the generic type parameter in advance, I can't use solutions like below, over the returned object:

method = method.MakeGenericMethod(typeof(SomeClass));
var result = method.Invoke(service, null);

So, What I did was, using the dynamic type, and calling the property name over the returned object.

var method = octx.GetType().GetMethods().First(x => x.Name == nameof(octx.CreateObjectSet) 
&& x.DeclaringType == typeof(ObjectContext));
var generic = method.MakeGenericMethod(type);
dynamic value = generic.Invoke(octx, null);// => This is the generic typed return value.
// below, Using dynamic will work, with the pitfal of no compiler checks
EntitySet set = value.EntitySet;

Now, That was working perfectly fine until I decided to copy the code over to another solution, where I'm creating an open source extensions library for Entity Framework (EntityExtensions).

The same line of code that works fine in the previous solution, No longer works in the new solution!!! It reports: System.Data.Entity.Core.Objects.ObjectQuery' does not contain a definition for 'EntitySet'

What drove me crazy is, I'm able to view the actual property in the debugger! A property with the same name is there, but It simply throws an exception about not having this property on the dynamic object!

I'm writing the answer as an answer instead of here just for clarity.





Aucun commentaire:

Enregistrer un commentaire