c# - How to map ICollection<Type> to extending class -
i have created data model looks this:
// base data item class public class dataitem { [key] public int id { get; set; } public string name { get; set; } [required] public int dataitemgroupid { get; set; } public virtual dataitemgroup dataitemgroup { get; set; } } // extending data item class public class specificdataitem : dataitem { public string extrafield { get; set; } } // class grouping of data items may extended or not. public class dataitemgroup { [key] public int id { get; set; } public string name { get; set; } public virtual icollection<dataitem> dataitems { get; set; } public virtual icollection<extendeddataitem> extendeddataitems { get; set; } }
my issues when use entity frameworks lazy loading, dataitemgroup.dataitems populated items, including extendeddataitems, dataitemgroup.extendeddataitems empty.
i suspect missing mapping of sort, don't know should looking for. ideas on how dataitemgroup load both dataitems , extendeddataitems collections through lazy loading?
you create 1 relationship using base class (dataitem
):
public class dataitemgroup { [key] public int id { get; set; } public string name { get; set; } public virtual icollection<dataitem> dataitems { get; set; } }
when need filter elements specific type inherit dataitem
, use oftype<t>
method , example:
var ditemgroup= dc.dataitemgroups.firstordefault(); var extendeddataitems=ditemgroup.dataitems.oftype<extendeddataitem>();
Comments
Post a Comment