Let’s say that I have these models:
public class TextDocument
{
public int Id { get; set; }
public string Name { get; set; }
public virtual List<Paragraph> Paragraphs { get; set; }
}
public class Paragraph
{
public virtual TextDocument Document { get; set; }
public int Order { get; set; }
public string Text { get; set; }
}
public class Image
{
public virtual Paragraph Paragraph {get; set; }
public virtual TextDocument Document { get; set; }
public string Url { get; set }
}
And now, I need to navigate in TextDocuments, Paragraphs, Images, Paragraphs in TextDocuments, Images in Paragraphs, Images in TextDocuments, etc.
How do I “connect” the models?
What I’m asking is:
- How to make the DataContext? Only for the TextDocument?
- With this, how do I get, for example, all Images without knowing the Id’s, etc?
The question is not clear.
Also your model is a bit strange.
TextDocumentdoes not include a list ofImages. ButImageboth includes back navigations toParagraphandTextDocument. I think you need to addImagelist toParagraphand removeTextDocumentfromImage. This way you will enableDocuments to havePharagraphs andPharagraphs to haveImages;To create a context create a class deriving from DbContext, and add your entity sets;
to fetch the images of a specific text document with id equals to 3;
to select all images;
Refer to this blog post which has a good tutorial about EF Code First.