ef code first - Entity framework TPT hierarchy not working for deep hierarchy when reading values from database -
i have 3 tables:
duty task :duty processtask : task
i'm using tpt hierarchy , seems fine when creating database. when read values database, i'm not getting values duty table. query:
var tasks = _dbcontext.duties.oftype<task>() .oftype<processtask>() .where(c => c.parentid == id);
only values task , processtask returned, although sql query returned correct values (i checked sql profiler).
why not map first abstract class' fields? appreciated.
my source code:
public class duty { public int id { get; set; } } [table("task")] public class task : duty { public int id { get; set; } public int? duration { get; set; } public int? noofrepeats { get; set; } } [table("processtask")] public class processtask : digibob.model.governance.duties.task { public int processtasktypeid { get; set; } public virtual processtasktype processtasktype { get; set; } //processtasks public int? parentid { get; set; } [foreignkey("parentid")] public virtual processtask parent { get; set; } //children [required(errormessage = "a short description required")] [maxlength(150, errormessage = "the maximum length short description 150")] public string shortdescription { get; set; } } public class mycontext : dbcontext { public dbset<duty> duties { get; set; } }
your query wrong... - otherwise code should work fine
what you're doing - returning
processtask
-s. (oftype selects tasks - reapply oftype - , further narrow down.
// returns processtask-s ienumerable<duty> tasks = db.duties .oftype<task>() .oftype<processtask>() .tolist(); // returns task-s (including processes) tasks = db.duties .oftype<task>() // .oftype<processtask>() .tolist(); // returns duties (including tasks, processes) tasks = db.duties .oftype<duty>() .tolist();
Comments
Post a Comment