I am trying to add a new item into the database (with 1=>M relationship) and am stuck at selecting it in the method. How to connect this new contact to the existing account?
Method:
var contacts = _dataContext.Accounts.Select(a => a.Contacts.Where(c=>c.Email==email));
var newContact = new Contact();
newContact.FirstName = firstName;
newContact.SecondName = secondName;
newContact.Email = email;
_dataContext.Contacts.Add(newContact);
await _dataContext.SaveChangesAsync();`
Database structure:
public class Account
{
public int Id { get; set; }
public string AccountName { get; set; }
public List<Contact> Contacts { get; set; }
}
public class Contact
{
public int Id { get; set; }
public string FirstName { get; set; }
public string SecondName { get; set; }
public string Email { get; set; }
public int AccountId { get; set; }
public Account Account { get; set; }
}