Hi,

Is it possible to have an entityset that describes the relationship with a parent entity.

For instance:

Person has relationship with other Persons .. So we can create entityset of persons. But we would like to define the type of relationship ie.. son, dauther, mother, brother, cousin....

Will there need to be an intermediate entity to hold the relationship field? If the intermidiate entity is needed it adds a lot more work to maintain this new entity paring.

Thank you,

Richard

asked Aug 21 '13 at 16:51

rasxte's gravatar image

rasxte
20161617

edited Aug 21 '13 at 16:55


3 Answers:

Hey Richard,

Unfortunately, there is no way to specify any additional data for EntitySet, the only option is to use intermediate Entity, which would contain references between 2 persons and relationship kind as a separate persistent field.

To minimize the pain of pairing entities, consider using custom EntitySet implementation with extended API, e.g.

public MyEntitySet : EntitySet<PersonRelationship> {

  public void Add(Person child, RelationshipKind kind) {
    this.Add(new PersonRelationship(this.Session, child, kind));
  }

  public void Remove(Person child) {
    // Here you should find the corresponding relationship instance
    // and remove it from entity set
  }

  protected override OnRemove(PersonRelationship item) {
    // This 'event' is called during Remove & Clear methods.
    // In both cases intermediate entity is no more required. 
    // Removing it from database
    item.Remove();
  }
}

Hope that helps

answered Aug 22 '13 at 18:51

Dmitri%20Maximov's gravatar image

Dmitri Maximov
22111211

edited Aug 22 '13 at 18:53

Hi Dmitri,

Thank you for your response.

How would I go about having symmetric relationship. I am assuming I will have to do some association parings. The assumtion is that if Father has Child then the Child has Father.

Child = new Person() Father = new Person()

Father.RelationsShips.add(Child, RelationType.Child).

How would the Child get the Father association in it's relationships entityset Child.RelationShips should contain the Father entity. Could you show me the entity defintions to have this happen automatically.

Thank you,

Richard

answered Aug 23 '13 at 15:39

rasxte's gravatar image

rasxte
20161617

edited Aug 23 '13 at 15:41

Hey Richard,

Here is the simplest model and test. The main goal was to demonstrate the way you can design your model, I didn't follow any performance considerations. I ended up having 2 instances for describing a bidirectional relationship, one for forward direction and second one for reverse one.

P.S.
A piece of advice: as you keep asking questions in the same topic, you'd better do that as comments to a particular answer instead of creating new answers. Cheers.

answered Aug 23 '13 at 22:59

Dmitri%20Maximov's gravatar image

Dmitri Maximov
22111211

edited Aug 23 '13 at 23:13

Dmitri,

Thank you for such an comprehensive answer. I agree with you that two relationships instances are the best way to describe the bidirectional nature of these entities.

I appreciate your help very much.

Richard

(Aug 25 '13 at 14:53) rasxte rasxte's gravatar image

You are very welcome. In case the answer solves the issue, you might want to mark it as Accepted answer, so other forum users could easily find it. Thanks

(Aug 25 '13 at 14:59) Dmitri Maximov Dmitri%20Maximov's gravatar image

Hi Dmitri,

I tried to delete a person but I got the following error: Referential integrity violation on attempt to remove 'DOTestWeb.Model.PersonR', Key='PersonR, (2945)'. Association: Relationship-Relative-PersonR (AssociationInfo) Referencing Entity Key: Relationship, (2946, 2945) Referenced Entity Key: PersonR, (2945).

I thought that the deletions of relationships would happend automatically.

Thank you,

Richard

(Aug 25 '13 at 15:46) rasxte rasxte's gravatar image

Did you take the sample "as is" or just partially? The Person.Relationships field has the Association attribute configured so a Person instance's deletion would delete all its relationships.

(Aug 25 '13 at 15:59) Dmitri Maximov Dmitri%20Maximov's gravatar image

I do have the attributes...

[HierarchyRoot]  
public class PersonR : Entity  
 {  
     [Field, Key]    
     public int Id { get; set; }

     [Field]    
     public string Name { get; set; }

     [Field]    
     [Association(PairTo = "Person", OnOwnerRemove = OnRemoveAction.Cascade,  OnTargetRemove = OnRemoveAction.Clear)]    
     public RelationshipSet Relationships { get; private set; }

     public PersonR(Session session): base(session) 
     {

     }

 }
(Aug 25 '13 at 17:04) rasxte rasxte's gravatar image

Looks like Person type needs additional removal logic. Take a look at the updated gist (revision #3), I've added OnRemoving method. BTW, this could be a good idea to hide the entire Person.Relationships field (make it internal) and add relationship management methods (AddRelative, RemoveRelative) to Person class itself.

(Aug 25 '13 at 17:48) Dmitri Maximov Dmitri%20Maximov's gravatar image

Dmitri,

That worked... Are the attributes still necessary? [Association(PairTo = "Person", OnOwnerRemove = OnRemoveAction.Cascade, OnTargetRemove = OnRemoveAction.Clear)] public RelationshipSet Relationships { get; private set; }

If I hide the relationships and just AddRelative and RemoveRelative Methods, then I'll need other methods for count, search within the list methods, etc...

Thanks again,

Richard

(Aug 25 '13 at 19:01) rasxte rasxte's gravatar image

Yes, the attributes are necessary, keep them.

(Aug 25 '13 at 19:15) Dmitri Maximov Dmitri%20Maximov's gravatar image
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
toggle preview

Subscription:

Once you sign in you will be able to subscribe for any updates here

Tags:

×3
×2
×1
×1

Asked: Aug 21 '13 at 16:51

Seen: 8,897 times

Last updated: Aug 25 '13 at 19:15

powered by OSQA