entity framework - EF projections many to many not loading -
i have 4 classes defined follows:
public class operator : base { public string name { get; set; } public string url { get; set; } public icollection<address> addresses { get; set; } public icollection<contact> contacts { get; set; } public icollection<application.application> applications { get; set; } } public class address : base { public string street{ get; set; } public int? parentid { get; set; } public operator parent { get; set; } public icollection<application.application> applications { get; set; } } public class contact : base { public string name { get; set; } public int? parentid { get; set; } public operator parent { get; set; } public icollection<application.application> applications { get; set; } } public class application : base { [maxlength(300)] public string name { get; set; } public icollection<operator.operator> operators { get; set; } public icollection<operator.address> addresses { get; set; } public icollection<operator.contact> contacts { get; set; } } public class base { public int id { get; set; } //public int baseobjectid { get; set; } timezoneinfo _easternzone = timezoneinfo.findsystemtimezonebyid("eastern standard time"); private datetime _modifieddate; public datetime modifieddate { { return this._modifieddate; } set { this._modifieddate = datetime.specifykind(value, datetimekind.unspecified); this._modifieddate = timezoneinfo.converttimefromutc(this._modifieddate, _easternzone); } } private datetime _createddate; [databasegenerated(databasegeneratedoption.computed)] public datetime createddate { { return this._createddate; } set { this._createddate = datetime.specifykind(value, datetimekind.unspecified); this._createddate = timezoneinfo.converttimefromutc(this._createddate, _easternzone); } } public bool disabled { get; set; } } public class db : dbcontext { public dbset<ef.complaint.complaint> complaints { get; set; } public dbset<ef.category.category> categories { get; set; } public dbset<ef.action.action> actions { get; set; } public dbset<ef.medium.medium> mediums { get; set; } public dbset<ef.priority.priority> priorities { get; set; } public dbset<ef.complaint.comment> comments { get; set; } public db() { this.database.log = s => { system.diagnostics.debug.writeline(s); }; } protected override void onmodelcreating(dbmodelbuilder modelbuilder) { modelbuilder.properties<datetime>().configure(c => c.hascolumntype("datetime2")); modelbuilder.configurations.add(new complaintconfig()); modelbuilder.configurations.add(new categoryconfig()); modelbuilder.configurations.add(new actionconfig()); modelbuilder.configurations.add(new mediumconfig()); modelbuilder.configurations.add(new priorityconfig()); modelbuilder.configurations.add(new commentconfig()); base.onmodelcreating(modelbuilder); } }
operator
, contact
, address
can belong particular application. have structure this:
operator 1 - belongs app1 , app2
child contact 1 - belongs app1
child contact 2 - belongs app2
child address 1 - belongs app2
i trying build method returns a list of operators particular application , includes addresses , contacts of operator belong application
here query have concocted far
public ienumerable<operator> getforapp(string name) { return (context.operators.where(x => x.applications.any(y => y.name == name)) .select(x => new { x, addresses = x.addresses.where(y => y.applications.any(z => z.name == name)), contacts = x.contacts.where(y => y.applications.any(z => z.name == name)), applications = x.applications }).asenumerable() .select(n => n.x)); }
this works in sense basic members of operator
loaded addresses
, contacts
loaded , filtered correctly...what doesn't loaded applications
, can't figure out why. difference see addresses
/contacts
, operator
many-to-one , applications
, operator
many-to-many.
you must use lazy loading feature or include related object directly in query.
public class operator : base { public string name { get; set; } public string url { get; set; } public icollection<address> addresses { get; set; } public icollection<contact> contacts { get; set; } // adding virtual keyword ef generate proxy classes , // fetch actual objects when needed. public virtual icollection<application.application> applications { get; set; } }
or in query directly include application
:
public ienumerable<operator> getforapp(string name) { return (context.operators.where(x => x.applications.any(y => y.name == name)) .include(x=>x.applications) .select(x => new { x, addresses = x.addresses.where(y => y.applications.any(z => z.name == name)), contacts = x.contacts.where(y => y.applications.any(z => z.name == name)), applications = x.applications }).asenumerable() .select(n => n.x)); }
Comments
Post a Comment