I'm working with entity framwork code first I have two objects Project and Questionnaire.The project object has a liste of Questionnaire OneToMany association
This is my code :
The Projet Class :
public class Projet
{
[Key]
public int codeProjet { get; set; }
[Required(ErrorMessage="Veuillez entrer la designation du projet")]
[MaxLength(250)]
[Display(Name = "Designation")]
public String designation{ get; set; }
[Display(Name="Date début")]
public DateTime dateDebut { get; set; }
[Display(Name="Date fin ")]
public DateTime dateFin { get; set; }
[Column("desciption", TypeName = "ntext")]
[Display(Name="Desciption du projet ")]
public String desciption { get; set; }
[MaxLength(100)]
[Display(Name="Responsable du projet")]
public String responsable { get; set; }
[Display(Name = "Budget du projet")]
public double budget { get; set; }
public virtual ICollection<Questionnaire> questionnaires { get; set; }
}
The Questionnaire Class :
public class Questionnaire
{
[Key]
public int codeQuestionnaire { get; set; }
[MaxLength(250)]
[Display(Name="Desciption du questionnaire")]
public String designation { get; set; }
[Display(Name = "Questionnaire avec GPS ?")]
public bool avecGPS { get; set; }
public bool avecNote { get; set; }
//foreign key Projet entity
//[Required]
public int projetId { get; set; }
[ForeignKey("projetId")]
public virtual Projet projet { get; set; }
}
The context Class :
public class QuestContext :DbContext
{
public QuestContext() : base("name=QuestionnaireDbContext") { }
public DbSet<Projet> projets { get; set; }
public DbSet<Questionnaire> questionnaires { get; set; }
}
In my DAO Layer I want to add this Method
public void addQuestionnaireToProjet(int codeProjet, Questionnaire questionnaire) { }
My question is how I can add Questionnaire object to a Projet object that existe ? Thanks
DbSet.Add()?