entity-frameworkself-tracking-entities

EF 4.0 error of two entities with the same key with self tracking entities


I have two tables in which I save the relationship between persons. I want the hierarchy, so my two tables are:

Persons
-----------
IDPerson
Name



PersonRelation
-----------------
IDPersonRelation
IDPerson
IDRootPerson
IDParentPerson
Left
Right
Deep

IDPerson is an autonumeric and IDRelation is other autonumeric.

Then, in SQL Server, I create 3 relationships:

1) PersonRelation.IDPerson = Persons.IDPerson 2) PersonRelation.IDPersonRoot = Persons.IDPerson 3) PersonRelation.IDPersonParent = Persons.IDPerson

With the first relation, I want to know the information of the person of the actual node of the hierarchy tree (the name, telephone...)

With the second relation I want the information of the root of the kind of tree in which belong the actual node.

With the three realtion I want the parent.

Well, this is to have an idea, the most important is that I have two tables with three relations, because is the cause of the problem. The struct of the hierarchy is this.

Well, now with EF I use the following code to see if this works:

Persons person1 = new Persons();
person1.Name= "person01";

PersonRelation1 relation1 = new PersonRelations();
relation1.Left = 1;
relation.Right = 2;
relation.Deep = 0;
person1.PersonRelations.Add(relation1);

//I am using self tracking entities, so I apply changes
miContext.ApplyChanges<Persons>("Persons", person1);
miContext.SaveChanges();

This works fine, but if I try to add two persons with their relations, then I have the problem in the saveChanges in which it says that exists two entities with the same key. The code is the following:

Persons person1 = new Persons();
person1.Name= "person01";

PersonRelation1 relation1 = new PersonRelations();
relation1.Left = 1;
relation.Right = 2;
relation.Deep = 0;
person1.PersonRelations.Add(relation1);


Persons person2 = new Persons();
person2.Name= "person02";

PersonRelation2 relation2 = new PersonRelations();
relation2.Left = 1;
relation2.Right = 2;
relation2.Deep = 0;
person2.PersonRelations.Add(relation2);

//I am using self tracking entities, so I aply changes
miContext.ApplyChanges<Persons>("Persons", person1);
miContext.ApplyChanges<Persons>("Persons", person2);
miContext.SaveChanges();

Why with one register works and when I try to add two or more I have problems? because I am trying two add two diferents entities.

Thank you very much.


Solution

  • Well, the problem was that I need to add PersonRelation to the three collections of the Person entity, so I need to do the following:

    Persons person1 = new Persons();
    person1.Name= "person01";
    
    PersonRelation1 relation1 = new PersonRelations();
    relation1.Left = 1;
    relation.Right = 2;
    relation.Deep = 0;
    person1.PersonRelations.Add(relation1);
    person1.PersonRelations1.Add(relation1);
    person1.PersonRelations2.Add(relation1);
    
    
    Persons person2 = new Persons();
    person2.Name= "person02";
    
    PersonRelation2 relation2 = new PersonRelations();
    relation2.Left = 1;
    relation2.Right = 2;
    relation2.Deep = 0;
    person2.PersonRelations.Add(relation2);
    person2.PersonRelations1.Add(relation2);
    person2.PersonRelations2.Add(relation2);
    
    miContext.ApplyChanges<Persons>("Persons", person1);
    miContext.ApplyChanges<Persons>("Persons", person2);
    miContext.SaveChanges();
    

    I mean, that the Person entity has 3 collections, one for each relation, and it is needed to add the PersonRelation entity to the three collections.

    Thanks.