javascript - Navigation property not properly loaded -
in application, have 2 ef entities, user , company. these 2 entites have 1 1 relationship.
public class company : ientity { public virtual int id { get; set; } [required] public virtual user owner { get; set; } public virtual string name { get; set; } public virtual string address { get; set; } public virtual byte[] logo { get; set; } }
and user:
public class user : ientity { [notmapped] public virtual int id { get; private set; } [key] [required] public virtual string email { get; set; } public string name { get; set; } public virtual company company { get; set; } }
i query these objects following breeze js code:
var query = entityquery.from('currentuser').expand('company');
the web api controller has these 2 methods:
[httpget] public iqueryable<user> currentuser() { var username = ... return _breezecontext.context.users.find(sup => sup.email.equals(username)).asqueryable(); } [httpget] public iqueryable<company> company() { var username = ... return _breezecontext.context.companies.find(cmp => cmp.owner.email.equals(username)).asqueryable(); }
i can see coming server:
{$id:1, $type:user, mynamespace, id:0, email:something@something.com,…} $id: "1" $type: "mynamespace.models.user, mynamespace" company: {$id:2, $type:mynamespace.models.company, mynamespace, id:1, owner:{$ref:1}, name:my company,…} $id: "2" $type: "mynamespace.models.company, mynamespace" address: "somewhere in world" id: 1 logo: {$type:system.byte[], mscorlib,…} name: "company" owner: {$ref:1} email: "something@something.com" id: 0 name: "operator 2"
now in js code, can correctly see user
object not company
. user.company undefined
question 1. wrong code above , why breeze not deserialize objects correctly? 2. how can object graph build correctly on client side can bind user interface elements user
, user.company
properties?
i went through breeze samples no luck - appreciated
in ef one-to-one relation, fks have pks, shouldn't using email user pk.
even if doing (i.e. not using pks fks) possible, still have problems marked fk in user unmapped.
breeze associations require foreign keys , if don't have information in db, it's not possible breeze resolve relation.
so suggest set id in user , pk , set email unique. latter might want check unique constraint in entity framework code first.
Comments
Post a Comment