0

個人間の関係を保存する 2 つのテーブルがあります。階層が必要なので、2 つのテーブルは次のとおりです。

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



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

IDPerson は自動数値であり、IDRelation はその他の自動数値です。

次に、SQL Server で 3 つのリレーションシップを作成します。

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

最初の関係で、階層ツリーの実際のノードの人の情報を知りたい(名前、電話…)

2 番目の関係では、実際のノードが属しているツリーの種類のルートの情報が必要です。

3 つの realtion では、親が必要です。

まあ、これは考えを持っていることです.最も重要なのは、問題の原因であるため、3つのリレーションを持つ2つのテーブルがあることです. 階層構造はこうです。

さて、EF では、次のコードを使用して、これが機能するかどうかを確認します。

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();

これは正常に機能しますが、関係を持つ 2 人の人物を追加しようとすると、saveChanges に問題があり、同じキーを持つ 2 つのエンティティが存在するという問題が発生します。コードは次のとおりです。

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();

1 つのレジスターで機能するのに、2 つ以上追加しようとすると問題が発生するのはなぜですか? 私は2つの異なるエンティティを2つ追加しようとしているためです。

どうもありがとうございました。

4

1 に答える 1

0

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.

于 2012-08-17T13:50:10.823 に答える