0

My customer business object is an aggregate root for a collection of addresses.

public class Customer {
  Public Customer {Addresses = new List<Address>}
  public virtual List<Address> Addresses { get; set; }
  // other properties
}

Public class Address {
  public virtual Customer customer {get;set;}
  [NotMapped]

  // other properties
}

My context contains

public DBSet<Customer> Customers {get;set;}

but no DBSet for Address because I want the customer to be the aggregate root.

However when I use

Customer.Addresses.RemoveAll(x=>x.TaggedToDelete)

The Customer_Id in the Address table is set to null instead of the Address being removed from the database.

How do I remove the address from the database using the aggregate root?

I had a look at How to remove child one to many related records in EF code first database?

but it uses a DBSet for the child record.

Community
  • 1
  • 1
Kirsten
  • 15,730
  • 41
  • 179
  • 318

1 Answers1

0
var deleteQueue = customer.Addresses.Where(x => x.TaggedToDelete).ToArray();
 foreach (var a in deleteQueue)
 {
     var entry = connect.Entry(a);
     entry.State = EntityState.Deleted;
 }
Ori Lentz
  • 3,668
  • 6
  • 22
  • 28
Kirsten
  • 15,730
  • 41
  • 179
  • 318