c# 4.0 - Entity Framework non generic Include on dynamic ObjectSet -


i've ran problem while creating data access layer application. working derived entities in entity framework. if try create objectset derived entity, exception:

blockquote there no entitysets defined specified entity type 'type name'. if 'type name' derived type, use base type instead.

i've tried resolve problem via reflection. (if entity type derived entity type => objectset base type)

i found this: how can obtain objectset<t> entity-framework @ runtime t dynamic? haven't found how use include , on built objectset.

protected ienumerable<idataobject> getdata(type entitytype, expression<func<dynamic, bool>> whereclause, expression<func<dynamic, dynamic>>[] includes)     {         if (typeof(idataobject).isassignablefrom(entitytype.basetype))         {             return getdata(entitytype.basetype, whereclause, includes);         }         var contexttype = this.context.gettype();         methodinfo createobjectsetmethod = contexttype.getmethod("createobjectset", new type[] {}).makegenericmethod(entitytype);         // builds objectset<entitytype>          dynamic objectset = createobjectsetmethod.invoke(this.context, new object[] { });         dynamic query = objectset;         if (includes != null)         {             foreach (var include in includes)             {                 query = query.include(include);             }         }         if (whereclause == null)         {             whereclause = (item) => true;         }         query = query.where(whereclause);         return query.tolist().oftype<idataobject>();     } 

the code runs intended, long don't use includes , whereclause. when call function dont know resolved objectset (t-parameter) @ compile time.
there way use dynamic expressions in generic objectset?

thanks in advance.

the issue looks

query = query.include(include);

the var include expression<func<dynamic,dynamic>>

the include method access expecting string or expression<func<t, tproperty>>

have tried pass include string path? expect work.

otherwise need construct expression tree code since cant pass dynamic objects

 public static iqueryable<t> include<t, tproperty>(this iqueryable<t> source, expression<func<t, tproperty>> path) t : class; 

there dynamic linq library can check out. system.linq.dynamic can found @ following links http://msdn.microsoft.com/en-us/vstudio/bb894665.aspx intro here http://www.scottgu.com/blogposts/dynquery/dynamiclinqcsharp.zip

this easier alternative using text build expression trees. building expressions trees code more powerful , flexible harder.


Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -