Contacts
must be an ICollection
. EF doesn't support IEnumerable
as navigation properties, because it's impossible to add items to them.
This also means that you should change the code that creates contacts
:
var contacts = viewModel.Contacts.Select(c => new Contact
{
Title = c.Title,
Forename = c.Forename,
Surname = c.Surname,
Phone = c.Phone,
Email = c.Email
}).ToList(); // <= ToList() added
manpreet
Best Answer
2 years ago
I'm using MVC5 and EF6 Code First to create a new
Company
entity that can have manyContact
entities. However, only theCompany
record is being written to the database.Models:
Controller: