You can try to modify your entities to define composite key for PhoneNumber which would consist of PhoneNumber's Id and foreign key to Customer :
public class Customer
{
[Key]
public int CustomerId { get; set; }
public ICollection<PhoneNumber> phones { get; set; }
}
public class PhoneNumber
{
[Key, ForeignKey("Customer"), Column(Order = 1)]
public int CustomerId { get; set; }
[Key, Column(Order = 0), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int PhoneNumberId { get; set; }
public string Number { get; set; }
public string Type { get; set; }
public Customer customer { get; set; }
}
To configure composite key , use Fluent API configuration in DbConext's overriden method OnModelCreating :
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<PhoneNumber>()
.HasKey(c => new { c.CustomerId, c.PhoneNumberId });
}
The update your database : Add-migration , Update-Database .
Then after get entities in your api , you can update the Customer and child collections like :
Customer cs = new Customer();
cs.CustomerId = 1;
cs.name = "gg";
cs.phones = new List<PhoneNumber>();
cs.phones.Add(new PhoneNumber() { CustomerId = 1, Number = "12", Type = "gg" });
cs.phones.Add(new PhoneNumber() { CustomerId = 1, Number = "44", Type = "TT" });
var customer = db.customers.Include("phones").Single(o => o.CustomerId == cs.CustomerId);
customer.phones = cs.phones;
db.SaveChanges();