0

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

2
  • Have you tried using DbSet.Add()? Commented Mar 9, 2016 at 9:58
  • Yes to save a projet object I use using(var ctx = new QuestContext) {ctx.projets.add(projet);ctx.SaveChanges(); } Iwant to update projet by adding a new questionnaire objet to the projet Commented Mar 9, 2016 at 10:02

1 Answer 1

2

You can just add the questionnaire to the collection of questionnaires of the projet. If the relationship between projet and questionnaire is defined correctly, this will generate the correct foreign key automatically.

var existingProject = QuestContext.projets.Find(projetId);
var newQuestionnaire = new Questionnaire();
existingProject.questionnaires.Add(newQuestionnaire);
QuestContext.SaveChanges();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.