Reputation: 9963
I am doing something wrong when trying to remove a related record. I have a Customer model and a CustomerGroup model. A customer can belong to multiple customergroups.
I want to be able to add and remove a customer from a customergroup. Adding is the easy part, when I try to remove a customer from a customergroup the entire record is deleted from the customergroup table
To make things easy to understand here is my code.
[Table("CustomerGroups")]
public class CustomerGroupModel
{
[Key]
[DataMember]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid CustomerGroupId { get; set; }
[DataMember]
public Guid BusinessId { get; set; }
public virtual Business Business { get; set; }
[DataMember]
[Display(Name = "Group Name")]
public string GroupName { get; set; }
[ScriptIgnore]
public virtual List<Customer> CustomersInGroup { get; set; }
}
My Customer model
[Table("Customers")]
[DataContract]
public class Customer
{
[DataMember]
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public Guid Id { get; set; }
[DataMember(IsRequired = true)]
[Required(ErrorMessage = "First Name is required")]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[DataMember(IsRequired = true)]
[Required(ErrorMessage = "Last Name is required")]
[Display(Name = "Last Name")]
public string LastName { get; set; }
[DataMember]
public string FullName
{
get { return string.Format("{0} {1}", FirstName, LastName); }
}
[DataMember]
[Display(Name = "Groups")]
//change this as ICollection failed with restsharp to bindICollection<CustomerGroupModel> SelectedGroups { get; set; }
public virtual List<CustomerGroupModel> SelectedGroups { get; set; }
}
My FluentApi
modelBuilder.Entity<Customer>()
.HasMany(x => x.SelectedGroups)
.WithMany(x => x.CustomersInGroup).Map(
m =>
{
m.MapLeftKey("CustomerId");
m.MapRightKey("CustomerGroupId");
m.ToTable("CustomersInGroups");
});
As you can see I generate a table called CustomersInGroups.
When I try to update customer groups I want to delete all existing records then reinsert the newly selected ones (unless there is a better way of doing this)
using (var context = new MyContext())
{
var contextCustomer = context.Customers
.Include("SelectedGroups")
.SingleOrDefault(a => a.Id==customer.Customer.Id);
contextCustomer.SelectedGroups.ToList()
.ForEach(r => context.CustomerGroups.Remove(r));
context.SaveChanges();
}
When I execute this all records from the table CustomerGroups are deleted. How do I only delete the related records from the customersingroups table?
Any advice would be really appreciated
Upvotes: 1
Views: 887
Reputation: 1460
The reason the CustomerGroup records are deleted is that you remove them from the context that is translated as removing them entirely from the database. To remove only the relationships between the CustomerGroup instances related to the selected customer you have to assign an empty list to the SelectedGroups property:
contextCustomer.SelectedGroups = new List<CustomerGroups>();
context.SaveChanges();
Upvotes: 1