Is there an easy way to convert an inherited item to an object further up the hierarchy?

Posts   
 
    
Posts: 48
Joined: 14-Oct-2008
# Posted on: 05-Nov-2008 19:20:06   

This may not be an LLBL specific question; I'm not really sure... I have an object hierarchy using Target Per Entity. For example, class C derives from class B, and class B derives from class A. Another class Z has a collection of A, the items of which may be A, B, or C.

I have a situation where I want to convert some of those items that are C to items of B, losing the information that was contained in the class C, and only retaining the information in class B and class A.

Is there an easy way to do this? Or do I have to duplicate the object C into a new object B, remove the C object and add the B object? That would be a pain, but if it is how I have to do it then that's what I'll do.

I think that I could easily achieve what I want by just deleting the appropriate information out of the database table that holds C information, but that is kind of hack-ish and preferably avoided if possible.

-mdb

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 06-Nov-2008 06:23:22   

Hi mdb, please try this:

for (int i = 0; i< Z.TheAs.Count; i++)
{
    AEntity e = Z.TheAs[i];
    if (e is CEntity)
    {
        e = new BEntity(e.Fields);
    }
}
David Elizondo | LLBLGen Support Team
Posts: 48
Joined: 14-Oct-2008
# Posted on: 30-Nov-2008 22:53:58   

daelmo wrote:

Hi mdb, please try this:

for (int i = 0; i< Z.TheAs.Count; i++)
{
    AEntity e = Z.TheAs[i];
    if (e is CEntity)
    {
        e = new BEntity(e.Fields);
    }
}

Hmmm I don't see any constructor that would accept that... The only constructors I have are:

  1. BEntity()
  2. BEntity(Guid)
  3. BEntity(Guid, IPrefetchPath)
  4. BEntity(Guid, IValidator)

The PKID is a Guid, so that explains the Guid parameter. I'm using Self-Servicing - maybe that makes a difference?

While we are discussing this, I also have a need to UPcast an object.. eg promote a BEntity to a CEntity... I'm guessing that when we solve this issue, it will lead me to the answer to this question too.

-mdb

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 01-Dec-2008 10:25:35   

In general you can't change an entity type up or down the hierarchy. So you'll have to do it the hard way (copy data, remove the old entity, insert a new one with a different type).