1

I am just starting with Entity Framework and have the following problem:

I have a Site entity which contains a navigation property Paragraphs.

I have multiple entities (i.e. ImageParagraph, LinkListParagraph) that should be inherited form Paragraph.

I would like to query for a Site object and access its Paragraphs and work with them depending on their concrete type (i.e. ImageParagraph, LinkListParagraph).

It would work with a Table Per Hierarchy approach (with conditions), but then i would end up in a very dirty solution. Querying for the concrete type based on the Paragraph ID would work, but I hope there is a way better solution.

I want to query for a Site and show some Site specific data and the data in the Paragraphs (ImageParagraph, LinkListParagraph). I do not know how to set up the mapping in a way that it is possible to retrieve ImageParagraph, LinkListParagraph objects directly over the Paragraphs navigation property.

How would you solve this problem?

ER-diagram: http://img443.imageshack.us/img443/46/69863714ks0.jpg

alt text http://img443.imageshack.us/img443/46/69863714ks0.jpg

1 Answer 1

2

If you want to work with them based on their concrete type - that sounds like a case for polymorphism... can you (in a partial class) add a few methods?

partial class Paragraph {
    public abstract void Foo();
}
partial class ImageParagraph {
    public override void Foo() {/*code*/}
}
partial class LinkListParagraph {
    public override void Foo() {/*code*/}
}

Otherwise, if you want to filter the set, you can use the OfType extension method - i.e.

foreach(var imgPara in obj.Paragraphs.OfType<ImageParagraph>())
{ ... }

Perhaps you could add properties for the above too (in the parent object) - i.e.

partial class Site {
    public IQueryable<ImageParagraph> ImageParagraphs
    { get {return Paragraphs.OfType<ImageParagraph>(); }}

    public IQueryable<LinkListParagraph> LinkListParagraphs
    { get {return Paragraphs.OfType<LinkListParagraph>(); }}
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks.My question was unclear.I want to do: var site =context.Site.Include("Paragraphs").Where(c => c.SiteID == 1).First(); foreach(var paragraph in site.Paragraphs) { do something }; My problem is that I do not know the mapping in the EDM.(like with Table per Hierarchy with conditions)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.