sql - zero to many relationship mapping to model -
i'm having bit of issue mapping out model. have question model represents (obviously) question, , questiontype represents types of questions possible (text, multiple choice, list, multi-line text, , on...).
the issue i'm having right trying set options associated each of questiontype model question model. example, if questiontype list type, , list contained 3 elements, i'm trying join elements on question model. problem i'm having not questions need have questionoptions variable set. example, simple text question (not shown in code).
any suggestions on how achieve this?
question model
[table("questions")] public class question { [key] [databasegeneratedattribute(databasegeneratedoption.identity)] public int questionid { get; set; } [required] public string question { get; set; } public int questiontypeid { get; set; } [foreignkey("questiontypeid")] public virtual questiontype questiontype { get; set; } public virtual icollection<questionoptions> questionoptions { get; set; } }
questiontype model
[table("questiontypes")] public class questiontype { [key] [databasegeneratedattribute(databasegeneratedoption.identity)] public int questiontypeid { get; set; } [required] public string questiontype { get; set; } }
questionoptions model
public abstract class questionoptions { [key] [databasegeneratedattribute(databasegeneratedoption.identity)] public int optionid { get; set; } public int? questionid { get; set; } [foreignkey("questionid")] public virtual question question { get; set; } } [table("questiontype_list")] public class listquestion : questionoptions { [required] public string item { get; set; } }
questioncontext
public class questioncontext : dbcontext { public questioncontext() : base("defaultconnection") { } protected override void onmodelcreating(dbmodelbuilder modelbuilder) { database.setinitializer<questioncontext>(null); public dbset<question> questions { get; set; } public dbset<questiontype> questiontypes { get; set; } public dbset<listquestion> listquestions { get; set; } }
personally have questionoptions questions, if blank entry in table or perhaps sort of identifier allows know if multi-line or single line text.
Comments
Post a Comment