entity framework - EF get dbset name in runtime from Type -
purpose: need name of dbset name of entity typeof(useraccount) = "useraccounts". in runtime need mutual type loop , therefor not know illustration "useraccount". "name" typeof?
i have created dbcontext entities. have been googling time not seem working me because of type converting?
please see method getdbsetname in bottom of description.
i pretty new @ ef stuff - please help med issue described below ;-)
public class myentities : dbcontext { public dbset<useraccount> useraccounts { get; set;} public dbset<userrole> userroles { get; set; } public dbset<useraccountrole> useraccountroles { get; set; } }
defined list of type command output:
public static list<type> modellistsorted() { list<type> modellistsorted = new list<type>(); modellistsorted.add(typeof(userrole)); modellistsorted.add(typeof(useraccountrole)); modellistsorted.add(typeof(useraccount)); homecoming modellistsorted; }
the problem below using type - if utilize "useraccount" works , "useraccounts". not have "useraccount" in runtime in loop serie of types. have type list giving e
public static looplist() { list<type> modellistsorted = modellistsorted(); foreach (type currenttype in modellistsorted) { string s = datahelper.getdbsetname(currenttype, db); } }
here method giving me challanges ;-) meaning not compiling. saying missing assembly? know pretty pseudo can done smoothly?
public static string getdbsetname(type parmtype, myentities db) { string dbsetname = (db iobjectcontextadapter).objectcontext.createobjectset<parmtype>().entityset.name; homecoming dbsetname; }
the challenge here 2 reflection steps involved, 1 invoke generic createobjectset
method , 1 entityset
result. here's way this:
first, method:
string getobjectsetname(objectcontext oc, methodinfo createobjectsetmethodinfo, type objectsettype, type entitytype) { var objectset = createobjectsetmethodinfo.makegenericmethod(entitytype) .invoke(oc, null); var pi = objectsettype.makegenerictype(entitytype).getproperty("entityset"); var entityset = pi.getvalue(objectset) entityset; homecoming entityset.name; }
as see, first objectset
invoking methodinfo
representing generic method createobjectset<t>()
. find propertyinfo
entityset
property of generic type obectset<t>
. finally, property's value , name of obtained entityset
.
to this, first methodinfo
createobjectset<>()
(the 1 without parameters) , objectset<>
type
var createobjectsetmethodinfo = typeof(objectcontext).getmethods() .single(i => i.name == "createobjectset" && !i.getparameters().any()); var objectsettype = assembly.getassembly(typeof(objectcontext)) .gettypes() .single(t => t.name == "objectset`1");
in getobjectsetname
generic parameters specified concrete entity type, done these "makegeneric..." methods.
var oc = (dbcontextinstance iobjectcontextadapter).objectcontext; var entitytype = typeof(userrole); var name = getobjectsetname(oc, createobjectsetmethodinfo, objectsettype, entitytype);
in ef 6 these should using
s:
using system.data.entity.core.metadata.edm using system.data.entity.core.objects using system.data.entity.infrastructure using system.linq using system.reflection
entity-framework type-conversion dbcontext typeof dbset
No comments:
Post a Comment