0
public class PhoneNumber {
  public string Number {get; set;}
  public string Type {get; set;}
}

public class Customer {
   public ICollection<PhoneNumber> phones {get; set;}
}

I am posting(putting) a Customer object to my webapi. Whatever phone numbers are included in the posted object are the only phone numbers I want saved in the database. If there were previously numbers in the db for that customer, I want them deleted.

_context.Update(model);

I have tried the above method but it will only add or edit existing phone numbers. Numbers that exist in the db but not the posted object are not deleted.

jmorc
  • 560
  • 1
  • 3
  • 14
  • Where's your `delete` method ? – Eldho Jun 06 '19 at 18:06
  • @Eldho That's what I'm asking for. It seems very common to want to do what I'm trying to do so I was thinking it would be included in the framework. – jmorc Jun 06 '19 at 18:08
  • You could use `_context.MyEntities.Remove(yourModel); _context.SaveChanges()` but you need to attach your phones in order to remove the particular entites. You could also look for https://stackoverflow.com/questions/41960215/how-do-i-delete-multiple-rows-in-entity-framework-core – Eldho Jun 06 '19 at 18:11
  • @Eldho So there is not a way to replace a child collection with "one step" and instead I must manually delete the old ones and insert the new? – jmorc Jun 06 '19 at 18:29
  • why dont you update the values in child collection. – Eldho Jun 06 '19 at 18:44
  • @Eldho I have 10 or so collections on my object that I want to update the same way (by replacing the set with what's in the incoming object) I was hoping to avoid writing a bunch of similar code – jmorc Jun 06 '19 at 18:47
  • You could loop the objects and update the properties you want. – Eldho Jun 06 '19 at 18:52

1 Answers1

0

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(); 
Nan Yu
  • 26,101
  • 9
  • 68
  • 148