this making me feel idiot. entity framework supposed simple, yet can't sort out myself , i've got fundamental misunderstanding. hope doesn't turn out idiot-question - sorry if is.
three code-first objects, related 1 another.
public class schedule { [key, databasegenerated(databasegeneratedoption.identity)] public guid rowid { get; set; } public datetime start { get; set; } public datetime end { get; set; } public virtual icollection<charge> charges { get; set; } } public class charge { [key, databasegenerated(databasegeneratedoption.identity)] public guid rowid { get; set; } public decimal rate { get; set; } public type type { get; set; } } public class type { [key, databasegenerated(databasegeneratedoption.identity)] public guid rowid { get; set; } public string typename { get; set; } } when query this, want related types, so:
schedule currentschedule = _context.schedules .include("charges.type") .where(cs => cs.start < datewindow && cs.end > datewindow) .first(); in c#, has been working fine.
the problem arises because we're not stopping @ c#, passing data onto javascript library called breeze smooths out data operations @ client end. breeze has bug/feature demands ef relationships between objects specified @ both ends. when query above, don't end types, because relationship charge isn't directly specified.
so change this:
public class type { [key, databasegenerated(databasegeneratedoption.identity)] public guid rowid { get; set; } public string typename { get; set; } public virtual charge charge { get; set; } } because virtual navigation property, should enable breeze should go both ways across relationship without changing data structure. ef doesn't this. tells me:
unable determine principal end of association between types 'core.charge' , 'core.type'. principal end of association must explicitly configured using either relationship fluent api or data annotations
fair enough. can see how confusing. now, understanding if define foreign key in dependent class, has classes' primary key. change to:
public class type { [key, foreignkey("charge"), databasegenerated(databasegeneratedoption.identity)] public guid rowid { get; set; } public string typename { get; set; } public virtual charge charge { get; set; } } and seems work ... it's stopped loading type information when ask schedule. messing around includes doesn't seem @ all.
what's going on, , have done wrong?
you haven't added navigation property (type.charge) existing model/relationship. instead have changed relationship one-to-many one-to-one relationship because default if relationship has 1 navigation property ef assumes one-to-many relationship. change have configured one-to-one relationship.
those relationships have different foreign keys: original one-to-many relationship has separate foreign key in charge table (probably named type_rowid or similar). new relationship has foreign key @ other side in table type , primary key rowid. charges loading schedule don't have related type same primary key, hence no type loaded.
if want reproduce old (one-to-many) relationship navigation property @ other side must add collection type instead of single reference:
public class type { [key, databasegenerated(databasegeneratedoption.identity)] public guid rowid { get; set; } public string typename { get; set; } public virtual icollection<charge> charges { get; set; } }
Comments
Post a Comment