Problem with inheritance

Posts   
 
    
teizela
User
Posts: 13
Joined: 24-Jul-2004
# Posted on: 16-Sep-2006 14:30:53   

I'm often using the following construct to save an entity if it's not already there (selfservice):


AEntity newEnt = new AEntity(1111);
if (newEnt.IsNew)
{
  newEnt.Id = 1111;
  newEnt.Name = "abc";
  newEnt.Save();
}

This always worked fine for me. Now I have an Entity B that is a subclass of A an the code above (respectively a version saving BEntity) always fails with a "System.Collection.Generic.KeyNotFoundException". If I change it to:


BEntity newEnt = new BEntity(1111);
if (newEnt.IsNew)
{
  newEnt = new BEntity();
  newEnt.Id = 1111;
  newEnt.Name = "abc";
  newEnt.Save();
}

it works. But why is old way not working? Do you have any advice for me? Or is this the designated behaviour in an inheritence scenario?

Andreas

Chester
Support Team
Posts: 223
Joined: 15-Jul-2005
# Posted on: 17-Sep-2006 00:24:53   

If you do a search in the documentation on "inheritance" you'll see some helpful info regarding this situation. Especially "Generated code - Using the entity classes".

Saving the supertype entity in an inheritance situation isn't supported. You must save an instance of the subtype for inheritance to work properly. If you have TableA and can optionally have a record in TableB, you should instead use an optional 1-to-1 approach.

teizela
User
Posts: 13
Joined: 24-Jul-2004
# Posted on: 17-Sep-2006 00:54:44   

Thanks for the answer.

I'm not trying to save the supertype, I'm saving the subtype. Perhaps I was not clear enough about that in my post. The point is that I'm trying to check first whether this instance of the subtype already exists in the database. And it looks to me, that outside an inheritence scenario, it's possible to test for a specific primary key in the constructor and - if it doesn't exist - take the returned empty entity to save the new instance. In an inheritence scenario this seems not to be possible, I have to call the empty constructor explicitly to get an empty instance of the subtype that I can save. So if AEntity is not a subtype, the following code works, when it is a subtype, you have to uncomment the line otherwise it doesn't work:


AEntity newEnt = new AEntity(1111);
if (newEnt.IsNew)
{
//newEnt = new AEntity();
newEnt.Id = 1111;
newEnt.Name = "abc";
newEnt.Save();
}

Not really a big problem, I just wanted to understand why. simple_smile

Chester
Support Team
Posts: 223
Joined: 15-Jul-2005
# Posted on: 17-Sep-2006 19:28:19   

Hmm. I haven't tested it but that sure doesn't seem right to me. I'll see if I can duplicate the issue and get back to you...

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 18-Sep-2006 10:11:34   

Could you please paste a stacktrace? I can't reproduce it. Also with which version of llblgen pro do you see this? (runtime libs build nrs)

Frans Bouma | Lead developer LLBLGen Pro
teizela
User
Posts: 13
Joined: 24-Jul-2004
# Posted on: 18-Sep-2006 12:52:30   

Sure. But I'm leaving today for a business trip for one week. So I'll come back to this next week and try to send you the informations.