Note that there are some explanatory texts on larger screens.

plurals
  1. POEntity Framework 5 Many to Many Insert not working as expected
    text
    copied!<p>I am trying to insert data. I am inserting a new Company that has a new branch that has a new Company Contact. Firstly the schema. Notice the Many to Many relationship between CompanyBranch and CompanyContact. My issue is that the CompanyBranchID in <strong>CompanyContact</strong> <strong>always ends up as 0</strong> instead of the assigned CompanyBranchId. The Company and Branch data is inserted correctly. Its possible that relationships are not correct?</p> <p><img src="https://i.stack.imgur.com/YyK1g.png" alt="enter image description here"></p> <pre><code> [HttpPost] public ActionResult CreateCompany(FormCollection formData) { CompanyContact contact = new CompanyContact() { ContactName = formData["ContactName"], eMail = formData["Email"], LandLine = formData["LandLine"], Mobile = formData["Mobile"], Role = formData["Role"] }; CompanyBranch branch = new CompanyBranch() { BranchAddress1 = formData["BranchAddress1"], BranchAddress2 = formData["BranchAddress2"], BranchAddress3 = formData["BranchAddress3"], BranchAddress4 = formData["BranchAddress4"], BranchAddress5 = formData["BranchAddress5"] }; branch.CompanyContacts.Add(contact); Company newCompany = new Company() { Name = formData["CompanyName"] }; newCompany.Branches.Add(branch); newCompany.Branches[0].Company = null; newCompany.Branches[0].Students = null; db.Entry(newCompany).State = EntityState.Added; db.SaveChanges(); return RedirectToAction("Edit", "Student", new { id = int.Parse(formData["StudentId"]) }); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload