none
Linq and inherited objects RRS feed

  • Question

  • Hello,

    I've a problem using EF 4.1 code first and inherited objects. Basically, the problem is denomalyzing objects retrieved by a query, when I have a list of different object types inherited by same father (polymorphism).

    I need to make a unique query to read a collection of base and inherited types, denormalyzing properties also for subclasses. Is possible?

    An example:

    A class Document, and a class MyDocument that inherits from Document that adds a proprerty of type Customer (another custom object).
    We try this query:

        Dim q = (From p In dal.Documents.Include("Customer") Select p).FirstOrDefault
                   
    But we get this error:

    A specified Include path is not valid. The EntityType 'Documents' does not declare a navigation property with the name 'Customer'.

    This query works:

        Dim q = (From p In dal.MyDocuments.Include("Customer") Select p).FirstOrDefault

    But is not my solution, I want make one query for multiple inherited types, because I need to have a single list of objects returned by the query, and I need to access specific data from inherited types. The objects are disconnected from datacontext, so denomarlization may be executed during first query and cannot be executed with lazy loading.

    Any idea?

     

    thanks


    • Edited by Trapulo Monday, December 19, 2011 3:20 PM mispelled
    • Moved by Mike FengModerator Tuesday, December 20, 2011 6:48 AM ADO.NET Entity (From:Visual Basic Language)
    Monday, December 19, 2011 3:19 PM

All replies

  •  
    You should post to the forum below.
     
    Msdn.en-US.adodotnetentityframework
     
    Monday, December 19, 2011 3:23 PM
  • Hi Trapulo;

    In order for me to better understand your question can you please post and image of the database tables in question showing relationships and navigation properties.

    Thanks. 


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Monday, December 19, 2011 7:53 PM
  • Here there are the diagram of classes and their db schema.

    We want read the OrderDocuments objects (Orderdocuments table rows), denormalizing properties of subclasses like InvoiceOrderDocuments or ServiceOrderDocuments.Query result is a list of OrderdocumentsInvoiceOrderDocuments and so on (I obtain a list of  OrderDocument, and I can Directcast to InvoiceOrderDocument, etc. but related properties are not denormalized)

     

    thanks

     

     

     

    Tuesday, December 20, 2011 4:17 PM
  • Hi Trapulo;

    Looking over your question again you state the following :

     

    A class Document, and a class MyDocument that inherits from Document that adds a proprerty of type Customer (another custom object).

    We try this query:

    Dim q = (From p In dal.Documents.Include("Customer") Select p).FirstOrDefault

    But we get this error:

     
    This is the correct behavior. The class Documents does not have a property that points to a Customer object.

    Then you state this :

    This query works:

    Dim q = (From p In dal.MyDocuments.Include("Customer") Select p).FirstOrDefault

    But is not my solution, I want make one query for multiple inherited types

     
    Well this query will have all the properties of the Document class and all the properties of the derived class MyDocuments and the results should also include the Customer which is part of MyDocuments.

    I guess I do not understand your question. Can you post the code for the classes of the model and explain in another way what you need to do.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Wednesday, December 21, 2011 3:48 AM
  • We need to query data "from OrderDocuments". When Linq deserializes data from db, it returns a list of OrderDocuments that in fact are instances of OrderDocuments, InvoiceOrderDocuments, and so on, and that really good. So, I can call a "TypeOf" and a "Directcast", and access deserialized data "as they really are", not as the parent type that is defined in the query.

    My problem is that, after I use tpeOf to discover that a specific instance is, for example, an InvoiceOrderDocument and not a generic OrderDocument, and I cast it using Directcast to InvoiceOrderDocument type, I can access avery property that the specific object instance has, inherited from OrderDocument parent type or added in InvoiceOrderDocument child type, but I cannot access related data for properties that reference other types.

    For example

    dim myType as InvoiceOrderDocument = Directcast (objectReturnedByTheQueryOnDal_Orderdocuments, InvoiceOrderDocument)
    
    console.write (myType.CreationTimestamp) ' that works, easy
    console.write (myType.InvoiceID) ' that works too, great!
    console.write (myType.Invoice.ID) ' that doesn't work because InvoiceOrderDocument.Invoice has not beed deserialized
    


    Thanks

     

    Wednesday, December 21, 2011 1:46 PM
  • Hi Trapulo;

    I made a TPH with EF Code First where the derived type had a collection and I had no problem accessing the collection after type casting the base type into a derived type. Please post the class in the model and the DbContext class.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Wednesday, December 21, 2011 4:04 PM
  • Did you try using a "disconnected" object collections, or accessing the related data when the main object was inside the datacontext too?

    I need to deserialize all, so the object can be returned by a function with all related data.

    Thanks

     

    Friday, December 23, 2011 2:58 PM