I have a one-to-many uni-directional relationship between Contact and Phone defined like this:
class Contact {
int ContactId {get; set}
ICollection<Phone> Phones {get; set}
}
class Phone {
int PhoneId {get; set;}
string PhoneNumber {get; set;}
}
Now in the domain layer, i try to do the following:
someContact.Phones.Remove(somePhone);
and when i try to call context.SaveChanges()
i get an exception because the relationship is defined as Required (eg. a phone cannot exist without a contact).
How can i solve this without using a foreign key or a navigation property in Phone and without the need to call DbSet<Phone>.Remove(Phone)
before calling SaveChanges()
?