Reputation: 995
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
Upvotes: 1
Views: 1103
Reputation: 1062492
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>(); }}
}
Upvotes: 2