Answered EntityRef Inheritance Discriminator

  • Thursday, August 02, 2007 8:14 PM
     
     

    Whenever I create an association where the EntityRef instance carries an inheritance hierarchy generic type, the discriminator column is not considered.


     

    Code Snippet

    public class Customer : CustomerVendorBase { ... }

     

    public class Invoice

    {

    ...

    public Customer Customer

    {

    get

    {

    return customer.Entity;

    }

    set

    {

    ...

    }

    } private EntityRef<Customer> customer;

    }

     

     

     

    Calling invoiceInstance.Customer only constraints on the "ThisKey" and "OtherKey" columns.  This can cause "Sequence has more than one element..." errors.  Is this by design?

All Replies

  • Friday, August 03, 2007 6:14 AM
     
     

    Classes above the class with [Table] aren't considered part of the mapped hierarchy. Could this be what you're seeing?

  • Friday, August 03, 2007 2:11 PM
     
     

    That must be what I am seeing.

     

    So one can use the single table inheritance support like so:

     

    Code Snippet

    from customerVendor in dataContext.GetTable<CustomerVendorBase>()

    select customerVendor as Customer

     

     

     

    I figured that when the EntityRef instance is given a source, the data context could detect that the generic type is a decendant in a single table inheritance mapping, and provide a query like the following (as the source):

     

    Code Snippet

    from customerVendor in dataContext.GetTable<CustomerVendorBase>()

    where [association constraint]

    select customerVendor as Customer

     

     

     

    Basically, create the query using the class containg [Table], but cast to the type provided as the generic type.  The lack of support for this severely limits our usage of [Association].

     

  • Friday, August 03, 2007 4:26 PM
     
     Answered

    Yes, this is by-design.  Claiming in the mapping that an association is constrained to an inheritance sub-type does not mean that the association includes a sub-type filter, it means that you have promised that the associated items are all within the sub-type.

     

    For example, if I have Employee and Manager types, and a Manager is an Employee, the 'Manager' field of an employee and be said to refer to a 'Manager' type instance.  LINQ to SQL will not enforce this, but allow you to type the association this way for your ease of use.

     

  • Friday, August 03, 2007 4:34 PM
     
     

    Without the discriminator taken into consideration my association will pull both a record with code = "XYZ" and type = "C" (customer) as well as a record with code = "XYZ" and type = "V" (vendor)...

     

    ...and we all know how EntityRef instances handle more than one element in it's source sequence.

     

    So in this type of situation, we just can't have an association?  End of story?

     

  • Wednesday, March 19, 2008 5:15 PM
     
     
    You should be able to constrain the association if you want to;

    I find it hard to not use an association becasue there is no way to get the DataContext that Created the object from within the object unless you make your own reference that you set on a loop through the selected items.

    Like so:
    Participating Properties:
    JobAddress.parentID -> Job.ID, JobAddress.typeID = 2
    or
    JobAddress.parentID -> Job.ID, JobAddress.typeID => 2

    this fuctionality would be great