Suppose a customer has many phone numbers and a phone number has only one customer.
public class PhoneNumber : IValueObject {
public string Number {get; set;}
public string Type {get; set;}
}
public class Customer : IEntity {
public ICollection<PhoneNumber> phones {get; private set;} //ew at no encapsulated collection support
public void SetPhones(params PhoneNumber[] phones) {
this.phones.Clear();
this.phones.AddRange(phones);
}
}
If I do an EF mapping like this and run it, every time I set phone numbers it will create new PhoneNumbers but not delete the old ones. There are no other entities referencing phone numbers, I don't even expose it on my dbcontext, is there a way to tell EF that Customer
owns PhoneNumbers
completely and therefore if phone numbers were removed from the collection they should be deleted?
I realize that there's a dozen ways to hack around this problem, but this isn't a weird edge case, what's the "right" way to handle this.