EntityFramework 5 - Conflicting changes to the role 'x' of the relationship 'x' have been detected
Solution 1
I found the problem, after read this msdn post, I was thinking and found out what was happening, in my repository when I will update my entity, I was forgeting to set null all the related entities.
Old code:
var oldAnimal = context.Animals.Find(animal.Id);
if (oldAnimal != null)
{
oldAnimal.AnimalBreed = context.AnimalBreeds.Find(animal.AnimalBreed.Id);
oldAnimal.AnimalSpecie = context.AnimalSpecies.Find(animal.AnimalSpecie.Id);
oldAnimal.OwnerId = animal.OwnerId;
oldAnimal.ProtectorId = animal.ProtectorId;
oldAnimal.Castrated = animal.Castrated;
oldAnimal.DateBirth = animal.DateBirth;
oldAnimal.Gender = animal.Gender;
oldAnimal.Name = animal.Name;
oldAnimal.UpdateDate = DateTime.Now;
oldAnimal.Vaccinated = animal.Vaccinated;
oldAnimal.Weight = animal.Weight;
}
context.SaveChanges();
return animal;
new code:
var oldAnimal = context.Animals.Find(animal.Id);
if (oldAnimal != null)
{
oldAnimal.AnimalBreed = context.AnimalBreeds.Find(animal.AnimalBreed.Id);
oldAnimal.AnimalSpecie = context.AnimalSpecies.Find(animal.AnimalSpecie.Id);
oldAnimal.Owner = null;
oldAnimal.Protector = null;
oldAnimal.OwnerId = animal.OwnerId;
oldAnimal.ProtectorId = animal.ProtectorId;
oldAnimal.Castrated = animal.Castrated;
oldAnimal.DateBirth = animal.DateBirth;
oldAnimal.Gender = animal.Gender;
oldAnimal.Name = animal.Name;
oldAnimal.UpdateDate = DateTime.Now;
oldAnimal.Vaccinated = animal.Vaccinated;
oldAnimal.Weight = animal.Weight;
}
context.SaveChanges();
return animal;
Solution 2
I don't agree with the above answer. I am not sure whether it solved your problem permanently because the issue is not related with the null value assignment. The actual reason is related with DBContext
. When we go for any SaveChanges
the context needs to be dispatched properly in order to proceed with the next SaveChanges
to insert another record into DB on the same item with a different foreign key. You just need to add the below line after your "context.SaveChanges()
"
context.Entry(your object).State = System.Data.Entity.EntityState.Detached;
This will solve the conflicts. Multiple insertion with same context results in conflicts.
Apologize if my comments criticised your answer in any manner.
Related videos on Youtube
BetaSystems - Rodrigo Duarte
Updated on September 16, 2022Comments
-
BetaSystems - Rodrigo Duarte over 1 year
I have this model (Animal Model):
public int Id { get; set; } public int AnimalSpecieId { get; set; } public int AnimalBreedId { get; set; } public Nullable<int> ProtectorId { get; set; } public Nullable<int> OwnerId { get; set; } public string Name { get; set; } public virtual Owner Owner { get; set; } public virtual Protector Protector { get; set; }
Protector Model:
public int Id { get; set; } public string Name { get; set; } public string Address { get; set; } public string Phone { get; set; } public string CellPhone { get; set; } public string Email { get; set; } public virtual ICollection<Animal> Animals { get; set; }
Owner Model:
public int Id { get; set; } public string Name { get; set; } public string Address { get; set; } public string Phone { get; set; } public string CellPhone { get; set; } public string Email { get; set; } public virtual ICollection<Animal> Animals { get; set; }
When I insert this model at the first time, if
ProtectorID = 1
and
OwnerID = null
it's ok, but, and I try to update this model, changing to:
OwnerID = 1
and
ProtectorID = null
I get the error in title, someone can help me with that ?
-
BetaSystems - Rodrigo Duarte over 10 yearsWhat happens is that I have only one context, one
Entity
, it'sAnimal Entity
, with manyforeign keys
associated to it, only way, that I found up to now, it's make theEntity
associatednull
and updating theforeign entity
ID
, in theAnimal Entity
, with the real ID, in this caseEntity Framework
can update without inserting a new entity. -
BetaSystems - Rodrigo Duarte over 10 yearsBut thanks anyway, and you don't criticised in any way my answer... It's just your answer can't be applied in my problem.
-
vinmm about 6 yearsThis worked for me too. But what if I want to maintain that circular dependency ? i.e., i want oldAnimal.Owner to be set back to animal.Owner ?? I have this kind of requirement. In next steps, i will be accessing oldAnimal's owner as owner object itself. If i set it to null, i cannot access it. Any hints ? Let me know.