c# - Composite Key to navigation property -
i'm trying create table holding notes specific entity. have entity called notes i'd store pk of entity referencing. i'd have composite key of key , table name containing primary key. see example below:
public class lot { public int lotid { get; set; } public virtual icollection<note> notes { get; set; } } public class task { public int taskid { get; set; } public virtual icollection<note> notes { get; set; } } public class note { public int id { get; set; } public int entityreferenceid { get; set; } public string entitytype { get; set; } public string comment {get; set; } }
so in notes table there be:
id: 1 entityreferenceid: 1 entitytype:lot comment: "comment one, lot one"
id: 2 entityreferenceid: 1 entitytype:lot comment: "comment two, lot one"
id: 3 entityreferenceid: 1 entitytype:task comment: "comment one, task one"
it seems should possible in database, i'm not having luck model. can point me in right direction?
make note
class abstract, , create 2 derived classes, 1 lot notes, , other task notes this:
public abstract class note { public int id { get; set; } public string comment {get; set; } } public class noteforlot : note { public int lotid { get; set; } public virtual lot lot { get; set; } } public class notefortask : note { public int taskid { get; set; } public virtual task task { get; set; } }
notice how each subclass references correct parent object.
your lot
, task
classes need change reflect changes:
public class lot { public int lotid { get; set; } public virtual icollection<noteforlot> notes { get; set; } public lot() { notes = new hashset<noteforlot>(); } } public class task { public int taskid { get; set; } public virtual icollection<notefortask> notes { get; set; } public task() { notes = new hashset<notefortask>(); } }
entity framework automatically create column in notes table (to replace entitytype
property) distinguish between 2 kinds of notes. such column called discriminator
in database.
this feature called table-per-hierarchy inheritance. can google obtain more information it.
Comments
Post a Comment