c# - Entity Framework Code First - The entity or complex type cannot be constructed in a LINQ to Entities query -
first project using ef 6. have 3 tables, facility, ewc , facilitytoewc. each facility can have many ewc's.
public class facility { public int facilityid { get; set; } public string facilityname {get; set;} } public class ewc { public int ewcid { get; set; } public sting ewccode { get; set;} } public class facilitytoewc { [key] [databasegenerated(databasegeneratedoption.identity)] public int facilitytoewcid { get; set; } public int facilityid { get; set; } public facility facility { get; set; } public int ewcid { get; set; } public ewc ewc { get; set; } }
hope correct. problem need 1 method return each facility ewc codes in json format. have done
public class facilitydto { public int facilityid { get; set; } public ienumerable<ewc> ewc { get; set; } } public ienumerable<facilitydto> getfacilities() { var result = (from currentfacility in db.facilities select new facilitydto() { facilityid = currentfacility.facilityid, ewc = ewcdetail in db.facilitytoewcs ewcdetail.facilityid == currentfacility.facilityid select new ewc { ewccode = ewcdetail.ewc.ewccode } }); return result; }
when execute above method error in posts title. appreciated. thanks.
i had create 2 tables , ef auto created mapping table once placed mappingcode in onmodelcreating. see code working below.
protected override void onmodelcreating(dbmodelbuilder modelbuilder) { modelbuilder.conventions.remove<pluralizingtablenameconvention>(); modelbuilder.entity<facility>() .hasmany(x => x.ewc) .withmany(x => x.facility) .map(x => { x.totable("facilitytoewc"); x.mapleftkey("facilityid"); x.maprightkey("ewcid"); }); base.onmodelcreating(modelbuilder); } public class facility { public int facilityid { get; set; } public string facilityname {get; set;} public icollection<ewc> ewc { get; set; } } public class ewc { public int ewcid { get; set; } public sting ewccode { get; set;} public icollection<facility> facility { get; set; } }
Comments
Post a Comment