1

I don't know how to properly title this question, so I did my best to come up with a descriptive title.

Basically, I have an Entity Framework entity that looks like the following ...

public class LegalDocument
{
    public int ExampleId { get; set; }
    public virtual ICollection<LegalDocument> LegalDocuments { get; set; }
}

The LegalDocument entity needs to be able to have references to other LegalDocuments. In the reverse direction, I need to be able to see what other LegalDocuments reference this LegalDocument.

So it's like several Entity Framework navigational properties, but I don't know how to specify that one of the properties is for LegalDocuments referenced within this LegalDocument, versus the other LegalDocuments that reference this one.

Any ideas?

rareyesdev
  • 2,337
  • 1
  • 25
  • 43
Ryan
  • 867
  • 9
  • 23
  • did you create a self refence in your database/codefirst it normally should create the many (collection) and at same time a parent (class), and this should be what you are looking for. – Rand Random Apr 23 '14 at 20:22
  • possible duplicate of [Entity Framework many-to-many self-reference](http://stackoverflow.com/questions/9640000/entity-framework-many-to-many-self-reference) – Daniel Brückner Apr 23 '14 at 21:41

2 Answers2

0

You can maintain the relationship in code having this method in the LegalDocument class.

public void RelateDocument(LegalDocument document)
{
    LegalDocuments.Add(document);
    document.LegalDocuments.Add(this);
}

Use it to relate documents instead of using LegalDocuments.Add() directly.

This way LegalDocuments navigation property links to documents that current instance added and documents that added the current instance.

rareyesdev
  • 2,337
  • 1
  • 25
  • 43
0

This ended up being easily doable using the fluent API's HasMany().WithMany().

Ryan
  • 867
  • 9
  • 23