Ask a questionAsk a question
 

AnswerHow to access derived entities in code

  • Wednesday, August 20, 2008 1:05 PMJamie Kindred2 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

     

    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

  • Wednesday, August 20, 2008 1:37 PMMatthieuMEZIL Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    You should add some properties on your ObjectContext like

    Code Snippet

    public IQueryable<MyDerivedEntity> MyDerivedEntities

    {

      get { return myBaseEntitySet.OfType<MyDerivedEntity>(); }

    }

     

     

     

All Replies

  • Wednesday, August 20, 2008 1:37 PMMatthieuMEZIL Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    You should add some properties on your ObjectContext like

    Code Snippet

    public IQueryable<MyDerivedEntity> MyDerivedEntities

    {

      get { return myBaseEntitySet.OfType<MyDerivedEntity>(); }

    }

     

     

     

  • Wednesday, August 20, 2008 1:50 PMJamie Kindred2 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

     

    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

     

     

  • Wednesday, August 20, 2008 1:54 PMMatthieuMEZIL Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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.  

  • Wednesday, August 20, 2008 2:45 PMJamie Kindred2 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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 Snippet

    Dim departmentQuery As ObjectQuery(Of Department) = schoolContext.Department.Include("Course").OrderBy("it.Name")

     

     

    I find that pretty confusing. Are there other valid prefixes?

     

    Thanks,

    JK

  • Wednesday, August 20, 2008 2:52 PMMatthieuMEZIL Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    it represents the parameter. In this case, you can also do:

    Code Snippet

    Dim 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 Snippet

    OrderBy(d => d.Name)

     

     

    You can also use a LINQ syntax:

    Code Snippet

    From d in schoolContext.Department.Include("Course") _

    Order by d.Name

     

    in VB (or something like that)

     

    or

     

    Code Snippet

    from d in schoolContext.Department.Include("Course")

    order by d.Name

    select d

     

     in C#.

     

  • Wednesday, August 20, 2008 4:33 PMJamie Kindred2 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    But why "it"? Where does it come from? Is that the default parameter name?

  • Thursday, August 21, 2008 2:06 PMMatthieuMEZIL Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

     Jamie Kindred wrote:
    I thought that the designer would do this automagically through codegen.

     

    I did it here.