This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Hi all, I'm having some trouble with querying multiple subclasses I could use a hand with. Below is an example of what I've done in my objects with inheritance.
Now, on my UsagePlan, PrepaidPlan, and RentalPlan I have the following annotation at the top of the class respectively [Discriminator(Attribute = "planClassId", Value = 1] [Discriminator(Attribute = "planClassId", Value = 2)] [Discriminator(Attribute = "planClassId", Value = 3)]
I'd like to be able to return all user plans with a single query. However when I perform a Repository.Find<UserPlan>, I receive the error Mindscape.LightSpeed.LightSpeedException: Descendent type [UserPlan] of type [Plan] is not discriminated.
Is it possible for me to have more than 1 level of persisted inheritance?
Thanks, Todd |
|
|
It is definitely possible to have more than one level in a STI hierarchy e.g. Plan -> UserPlan -> PrepaidPlan. LightSpeed will just "OR" the discriminators together. However it looks like we have a bug where LightSpeed needs the intermediate base classes to have discriminators as well as just the leaf classes, even if the intermediate base class is abstract. I will see if we can fix this, but as an interim solution you should be able to work around it using a dummy discriminator e.g. private const int DummyDiscriminator = 999999; // DO NOT USE IN DB [Discriminator(Attribute = "planClassId", Value = DummyDiscriminator)] |
|
|
Hello Todd, I've started to look at this and I think we're probably not going to address this unless absolutely necessary. The reason is that when LightSpeed sees: abstract class UserPlan : Plan with no discriminator, it will assume that the derivation is CTI and try to map it to a UserPlan table, rather than the Plan table that you need. Therefore we would need to apply some sort of attribute to it anyway to indicate that the UserPlan : Plan inheritance is STI even though no discriminator value will ever map to UserPlan (only to its derived classes). Given that this would partially duplicate the meaning of DiscriminatorAttribute, and would require separate designer support, we are reluctant to add such an attribute unless more customers run into this problem. Is it acceptable for you to use the dummy discriminator workaround? |
|
|
Hi Ivan, The dummy work around is fine with me. I've defined a inheritance with the following classes, but I can't create an object from data without a persistence error. Here is my error
[error] System.InvalidCastException: Specified cast is not valid. [/error]
public abstract class Plan{ //LS Disctimator types }
[Discriminator(Attribute = "ClassId", Value = ClassDiscriminator.AirwaysPlan)] .. required props here }
Here is my table data from the Plans Table ID: E32E8F8A-04C3-4E05-BB6E-D533F4458CC2
As you can see I have a matching class ID to my enum type. Any idea why I'm getting this error? My "AdministratorPlan" entityholder is of type "Plan". Latest stable LS 2
Thanks, Todd
|
|
|
The error message implies that the problem is with the AdministratorPlanId field (i.e. not with the AdministratorPlan EntityHolder, but with the foreign key field that underlies that association). My guess would be that the type of Spider.AdministratorPlanId in the entity model does not match the type of the PlanId column in the Spider table. Could you check these two types? |
|
|
Hi Ivan, You're right. I forgot to mark the corresponding Guid as nullable. I've been doing some Java recently and I spaced the "?" syntax for nullable objects when switching back to .NET
Thanks, Todd |
|