asp.net mvc 4 - Inserting values in master + child tables via Entity Framework -


I have 2 tables and have been automatically generated for both primary keys.

  Public partial class responses {public responses} {this.Answers = new HashSet & lt; Answer & gt; (); } Public Integer responseID {get; Set; } Public Systems Datetime create_dt {get; Set; } Public Virtual Ilectronics & lt; Answer & gt; Answer {Get; Set; }}  

responseID is an auto-gated primary key

  public partial class answer {public at answerID {get; Set; } Public Integer responseID {get; Set; } Receive Public Intestine Question ID { Set; } Public string reply {get; Set; } Public Virtual Question Questions {get; Set; } Public Virtual Response Response {get; Set; }}  

My attempt to add answers and answers at the same time

  Public Zero addAnswers (list  a) { Reply RP = new reply (); Rp.create_dttm = DateTime today; Db.Responses.Add (RP). Answers.Add (ANS); Db.SaveChanges (); }  

I want to add an answer at the same time because the response is inserted because ResponseID is automatically inspired and I can not find it Can answercode to link it to the answer table. Please note that a ResponseID is associated with many questions .......

Thank you in advance

Your code looks fine except for the fact that you do not use correctly ans archive To add each object answer , you should use the foreach loop:

  foreach (reply in reply reply) {RP. Answers.Add (answer); }  

In addition, you should respect naming conventions: You often start using plural, eg Perhaps responses should be named response , but I can be wrong, it depends on the words of all your institutions ...


Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -