How to access derived entities in code
I set up table-per-type inheritance using the MSND sample for coursemanager where I have an abstract base entity called course, and two derived entities called onlinecourse and onsitecourse.
How do I access these derived entities from my application code? I am able to access the abstract base class but not the two derived classes. I found an example that uses an ObjectDataSource, but I am more interested in the code-behind approach.
Please let me know if you need more details.
Thanks,
JK
Answers
You should add some properties on your ObjectContext like
Code Snippetpublic IQueryable<MyDerivedEntity> MyDerivedEntities
{
get { return myBaseEntitySet.OfType<MyDerivedEntity>(); }
}
All Replies
You should add some properties on your ObjectContext like
Code Snippetpublic IQueryable<MyDerivedEntity> MyDerivedEntities
{
get { return myBaseEntitySet.OfType<MyDerivedEntity>(); }
}
Interesting. I thought that the designer would do this automagically through codegen. Doesn't that make sense? If I create a derived type, that probably means that I want to use it in my code. The tool should create the interfaces by default, especially given the fact that the derived entity is marked as public in the designer.
As a work around, by following your suggestion, should I do that in a separate file so my changes are not lost if I regenerate the model?
Thanks,
JK
Of course. Classes generated by the designer are all partial. So of course, you should add these properties in a file which won't be reseted when you will change your designer.
Moreover, I am sure that you can effectively change the default code generator to add this automatically.
Thanks again Matthieu.
Maybe you can answer this one as well: what does the prefix "it" represent in Entity SQL (if that is in fact what the following code block is referred to)?
Example:
Code SnippetDim departmentQuery As ObjectQuery(Of Department) = schoolContext.Department.Include("Course").OrderBy("it.Name")
I find that pretty confusing. Are there other valid prefixes?
Thanks,
JK
it represents the parameter. In this case, you can also do:
Code SnippetDim departmentQuery As ObjectQuery(Of Department) = schoolContext.Department.Include("Course").OrderBy(Function(d) d.Name)
I am not sure about the Function syntax, I am not a VB expert.
In C#, you can do :
Code SnippetOrderBy(d => d.Name)
You can also use a LINQ syntax:
Code SnippetFrom d in schoolContext.Department.Include("Course") _
Order by d.Name
in VB (or something like that)
or
Code Snippetfrom d in schoolContext.Department.Include("Course")
order by d.Name
select d
in C#.
But why "it"? Where does it come from? Is that the default parameter name?
Jamie Kindred wrote: I thought that the designer would do this automagically through codegen. I did it here.


