asp.net mvc - Code First Entity / MVC4 - why aren't my models hydrated? -
i can not figure out i'm doing wrong here. new mvc , new entity, know that's holding me back. time call authuser, authrole nothing, end doing like:
authuser.authrole = db.authroleset.find(2) 'authroleid of 2 = user
this feels clunky me. how property role user?
here's class structure: public class authuser 'class globals dim db new authusercontext
'properties public property authuserid() integer <required()> _ <display(name:="user name")> _ <domainuservalidation()> _ public property username() string <display(name:="current role")> _ public property authrole authrole end class public class authrole public property authroleid() integer <required()> _ <display(name:="role name")> _ public property rolename() string <required()> _ <display(name:="is administrator")> _ public property isadministrator() boolean <required()> _ <display(name:="is active")> _ public property isactive() boolean <required()> _ public property authuser icollection(of authuser) end class public class authusercontext inherits dbcontext public property authuserset() dbset(of authuser) public property authroleset() dbset(of authrole) end class
you have 2 options (sorry c# syntax):
1 - lazy load authrole
when need - this, authrole
property needs declared virtual
public virtual authrole {get;set;}
now, when/if try access authrole
, ef database. work need have dbcontext.configuration.lazyloadenabled = true
another alternative eager load using query this:
var myuserwithrole = mycontext.authusers.include("authrole").firstordefault(x=>x.id == userid);
this user , role database.
Comments
Post a Comment