0

I'm trying to update many to many relationship using EF code first. Whenever I update I get this error, DietaryListings in the error is the join table created by migrations:

Violation of PRIMARY KEY constraint 'PK_dbo.DietaryListings'. Cannot insert duplicate key in object 'dbo.DietaryListings'. The duplicate key value is (1, 52). The statement has been terminated.

I realize that there is already existing duplicate data in DietaryListings before the update. I just don't understand why EF isn't allowing the update using the mapped relationship between Listing and Dietary. Shouldn't EF be updating the relationships if they change? Do I need to manually delete duplicate records in that join table before doing an EF update to the entity?

In my MVC Controller I'm trying to update the existing object with the model data passed from a razor view using this:

//listing = updated model from razor view
//listingExisting = listing from db service 
foreach (var dietary in listing.Dietaries)
{
    if (dietary.Selected)
    {
        listingExisting.Dietaries.Add(dietary);
    }
}
listingExisting.ObjectState = Repository.Pattern.Infrastructure.ObjectState.Modified;
_listingService.Update(listingExisting);

Listing class:

public partial class Listing : Repository.Pattern.Ef6.Entity
{
    public Listing()
    {   
        this.Dietaries = new List<Dietary>();  
    }
    public int ID { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Dietary> Dietaries { get; set; }
}

Dietaries class:

public partial class Dietary: Repository.Pattern.Ef6.Entity  
{
    public Dietary()        
    {            
        this.Listings = new List<Listing>();       
    }
    public int ID { get; set; }
    public string Name { get; set; }     
    public virtual ICollection<Listing> Listings { get; set; }         
}

I've looked at all of these links and some where helpful but didn't work: link1 link2 link3 link4 link5 link6

Heinrich
  • 1,711
  • 5
  • 28
  • 61
  • 2
    If your idea is updating, why are you calling `listingExisting.Dietaries.Add(dietary);`? This is **adding** the object as if it was a new one, so EF is trying to insert a new one rather than updating. Also, if you want to update, what do you want to upadte, since `Dietary` class doesn't have any properties other than ID? – Alisson Reinaldo Silva Oct 26 '18 at 03:59
  • 1
    Do you have null property in listing.Dietaries ? If you want to update, should not be blank property in listing.Dietaries.Specially pk and fk objects. – hakantopuz Oct 26 '18 at 12:18
  • @Alisson I'm updating the `Dietaries` that a `Listing` has. So a `Listing` can have dietary1, dietary2...dietary10. There are currently 10 `dietaries` in the database so any `Listing` can have any number of `dietaries` from 0..10. I'm not updating any `dietary` entity, just the dietaries that a listing contains. For simplicity, I took all the fields out of my classes since only the relationship was being updated. I updated my question and added the name fields so they don't look like empty classes. – Heinrich Oct 26 '18 at 17:50

0 Answers0