vb.net - Generically retrieve EF object of Type? -
in ef, there way similar following?
'where obj ef model object in context public function getsomeobject(obj object, id long) object using db contextentities = new contextentities dim objtype = obj.gettypeofobject() dim someobject = db.objtype.find(id) 'do magic here on .... end using end sub this is, of course, simple example idea pass in ef model object function , use type in db.modelobjecttype.find(...) call. so, example, if pass in customer object, retrieves customer table. if pass in address object, retrieves address table.
the dbcontext class has non-generic method (and generic version, may provide better solution, depending on how calling code) dbcontext.set takes type of entity query parameter. using this, code be:
'where obj ef model object in context public function getsomeobject(obj object, id long) object using db contextentities = new contextentities dim objtype = obj.gettype() dim someobject = db.set(objtype).find(id) 'do magic here on .... end using end sub
Comments
Post a Comment