Saving Polymorphic data & accessing the subtype

Posts   
 
    
Posts: 48
Joined: 14-Oct-2008
# Posted on: 02-Dec-2008 01:02:00   

I must be missing something here - these both seem so simple.

First Question:

I'm loading an object in a hierarchy polymorphically, then setting some values on the subtype. When I save the object, the field reverts to what it was before the save. Here's some example code:


BaseTypeEntity obj = BaseTypeEntity.FetchPoloymorphic(null, id, null);
if (obj is SubTypeEntity)
{
    SubTypeEntity sub = obj as SubTypeEntity;
    sub.SubField = 100;
    sub.Save();
}
obj.Save();

When I'm in the debugger, and I call Save() on 'sub' (or on 'obj' if I comment out sub.Save()), I can watch the Save occur, and the debugger immediately shows the original value.

I've also tried Save(true) but no difference. What am I doing wrong?

Second Question:

In the designer of the BaseTypeEntity, I see a 'Field on Relation' called 'SubType' that seems to imply that I should be able to access obj.SubType to get the object cast as a SubTypeEntity, but it doesn't seem to be there. What am I missing?

-mdb

Posts: 48
Joined: 14-Oct-2008
# Posted on: 02-Dec-2008 01:09:02   

A quick followup... The following code doesn't work either:

SubTypeEntity sub = new SubTypeEntity(obj.Id);

That is, it loads the object, but the Save still reverts the object. I'm wondering if something is just hosed on my system.

-mdb

Posts: 48
Joined: 14-Oct-2008
# Posted on: 02-Dec-2008 01:11:39   

fuzzylintman wrote:

A quick followup... The following code doesn't work either:

SubTypeEntity sub = new SubTypeEntity(obj.Id);

That is, it loads the object, but the Save still reverts the object. I'm wondering if something is just hosed on my system.

-mdb

First Question - SOLVED!! Stupid me had a validator that was enforcing some business rules that was resetting the price. I realized this when a change to a different field worked, but this particular field didn't.

Second Question - still stands. simple_smile

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 02-Dec-2008 04:34:38   

fuzzylintman wrote:

In the designer of the BaseTypeEntity, I see a 'Field on Relation' called 'SubType' that seems to imply that I should be able to access obj.SubType to get the object cast as a SubTypeEntity, but it doesn't seem to be there. What am I missing?

No, the relation is useful in the case the entities don't belong to an inheritance hierarchy.

David Elizondo | LLBLGen Support Team