Enhance an entity to be of a sub type - possible?

Posts   
 
    
Sokon1
User
Posts: 97
Joined: 17-Jul-2006
# Posted on: 16-Apr-2007 17:14:03   

Hi, - I've got an entity of type A and an entity of type B which inherits from A. - I read from the DB an get an entity instance of A. - I want to modify the A entity instance in a way so i can save it as a B entity.

Now here's my question: Is it possible to "enhance" my entity instance? If yes, how?

It's no option to delete A an add a new B entity, which was my first idea.

If i used simple SQL i would add a row in the table which contains the additional fields of B with the same PK like the A entity instance - but I didn't find out how to do this with LLBLGen.

Thanks for every hint!

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 17-Apr-2007 05:49:34   

Hi, what TemplateSet (Adapter | SS) are you using? Also, what LLBLGen Version?

Is it possible to "enhance" my entity instance? If yes, how?

What exactly are you thinking about this? Maybe something like add a B entity when you save an A entity, in this case, what values will fill the B additional fields?

Please elaborate with a example and maybe a code snippet wink

David Elizondo | LLBLGen Support Team
Sokon1
User
Posts: 97
Joined: 17-Jul-2006
# Posted on: 17-Apr-2007 08:39:17   

Please refer to my signature for information about template set, version etc.

I want to enhance my Aentity so it is a Bentity. (B inherits from A)

I can't a add a B entity when I save an A entity, because A is a part of B.

Ok, perhaps this makes it clearer: This is a method which gets an collection of AEntities and checks if they're BEntities, too. If not, the AEntiteis should be "Enhanced" by the lngFKUserRole-Field, which is the only additional field in BEntity. AFter that the AEntity became a BEntity and will be saved.


...
ACollection aColl = new ACollection();
aColl.GetMulti(null);
BEntity hlpEnt;
foreach (AEntity ent in aColl) {
if (ent is BEntity ) { continue; }
   hlpEnt = (BEntity)ent; *
   hlpEnt = MakeMeBEntity(ent); **
   hlpEnt.LngPkUser = ent.LngPkMitarbeiter;
   hlpEnt.LngFkUserRole = 1;
   unitOfWork.AddForSave(hlpEnt);
}
...

*) I know this can't be done and i want to write a funktionalitiy which "enhances" the AEntity to be BEntity. **) Another approach, instead of line *. Please help me to find the MakeMeBEntity() method! simple_smile

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 17-Apr-2007 10:36:05   

In an inheritance hierarchy, once an entity is saved you can't change its type. So you will have to delete entity A and Add another entity B (which will add a row in the A table). Or the otherway around Add the new entity B before deleting the entity A, in case you wanted to move some references from the old entity to the new one.

Sokon1
User
Posts: 97
Joined: 17-Jul-2006
# Posted on: 17-Apr-2007 11:18:19   

Hi, thanks for your answers,

As I wrote before, I must NOT change data in the data table of entity A. It is a general table in another database, where I've only got the permission to read. I use the specialized entity B to enhance the funtionality for the object provided by entity A.

So if there is no possibility to "enhance" my entity, and if I can't modify the data of the parent entity, I see only one other solution: To write a stored procedure, which adds rows to the data table of entity B, with the same PK like the "rest" of the entity in the datatable of A.

Because I modelled the 1:1-relation and inheritance manually in the LLBLGen-designer (tables are in two different dbs), the generated code should fetch only B-entities after "Enhancing" the data by running the sproc.

Do you agree?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 17-Apr-2007 11:38:36   

That's a possible solution indeed. Another option is to not use inheritance and let it just be a 1:1 referential relation. This way you can add an entity B that refers to an entity A.

Sokon1
User
Posts: 97
Joined: 17-Jul-2006
# Posted on: 18-Apr-2007 11:00:04   

Hi, thanks for your help! I solved it with a sproc - this was much easier to implement... simple_smile