Entity Framework Conflicting changes to the role of the Relationship have been Detected

Asked

Viewed 168 times

1

I have two objects that use a third in common and I have a problem in routine:

1) Creating a New Manufacturer

var fab = new Fabricante();
DBSet.Add(fab)   
//Sem SaveChanges

Then searching for the existing item and creating a new one, both of which use the same manufacturer not yet saved

var ItemProcesso = GetItem(1)
ItemProcesso.Fabricante = fab;

var adc = new Adicao()
adc.Fabricante = fab;
ItemProcesso.Adicao = adc;

Now before saving I need to know which fields are being changed from Then and that’s when the error occurs below:

var entryPro = Context.Entry(entity);

Error: Conflicting changes to the role 'Itemprocesso_fabricante_target' of the Relationship 'Portal.Infra.Data.Contexto.Itemprocesso_fabricante' have been Detected.

Now if I comment one of the 2 below the problem does not occur.

ItemProcesso.Fabricante = fab;
adc.Fabricante = fab;

NOTE: if I pass the Fabricanteid (object already saved) the problem does not occur

Does anyone know if there is any way to perform this operation without error? hint suggestions will also be welcome.

  • I believe it is necessary to even save the manufacturer before using it as a relationship in the two other individuals. In this case, you would make a context transaction to only effect the changes if everything went right. Technically I do not know what would be the justification, have to see in the documentation of the Entity framework what he talks about these situations, but I would bet that his mechanism is using the relationship of the new instance with the existing entity to infer how many records of Fabricante should go to the bank.

  • If my line of reasoning is not wrong, so is my mistake should not happen if you use two different instances in the relationship, but also two records will be created in the database.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.