EntityFramework 5 - Conflicting changes to the role 'x' of the relationship 'x' have been detected

半城伤御伤魂 提交于 2019-12-12 14:19:09

问题


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 ?


回答1:


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.




回答2:


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;


来源:https://stackoverflow.com/questions/19408041/entityframework-5-conflicting-changes-to-the-role-x-of-the-relationship-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!