What have I missed?

Posts   
 
    
mfas
User
Posts: 6
Joined: 27-Jun-2006
# Posted on: 16-Oct-2006 14:06:23   

Hi,

I'm trying to figure out this exception I have begun to get when trying to save an entitycollection using the adapter method.

The database model is getting rather complex, so I can not pin it out in detail here.

The exception is as follows:

"An exception was caught during the execution of an action query: Cannot insert the value NULL into column 'CaseId', table 'PManagement.dbo.Case2TurbineUnitType'; column does not allow nulls."

What I'm trying to do is that I have an entity called "Case" (CaseId being the PK) and a collection attached to that called "Case2TurbineUnitType" (referencing Case by CaseId). I have coupled the two together when creating new objects at runtime by doing somthing like this:

dim c2tute as new Case2TurbineUnitTypeEntity() ' ' setting various properties ' oCase.Case2TurbineUnitType.Add(c2tute) 'oCase being the runtime object of Case using daa as new dataaccessadapter(true) daa.saveentitycollection(oCase.Case2TurbineUnitType, true, true) daa.closeconnection() end using

When trying to use a dataaccessadapter to save the Case2TurbineUnitType collection, I get the above exception. The program fails at the SaveEntityCollection line.

While debugging the project I have noticed that everything seems to be in order object wise, that CaseId is set correctly and the objects are in the correct structure.

One important thing to note is that I have circular references in the database, which means you can go round in circles from Case via Case2TurbineUnitType and some other tables and getting back to the very same Case object again. This is the case when the saveentitycollection is being run. Is this giving any problems LLBL wise?

If it is the case, is there then any way to say to LLBL to only save 2 or 3 levels deep in the object hierarchy when saving recursively? This would otherwise be an idea for future development.

I hope you can help me out here, because I'm getting crazier by the moment :-)

Jessynoo avatar
Jessynoo
Support Team
Posts: 296
Joined: 19-Aug-2004
# Posted on: 16-Oct-2006 15:44:51   

Hi,

your circular reference should be the problem.

When saving recursively, LLBLGen determines the dependencies between your objects to determine the saving order.

What you seem to have there is that you CaseEntity object references a new entity, which in turns references a chain of new entities leading to your Case2TurbineUnitTypeEntity.

It then analyses that that entity needs to be saved in the first place thus the breaking query.

If the chaining references don't allow null value, then there is simply no way you can have something inserted in your tables unless you disable temporarily the constraint enforcement. That would probably mean your design needs some changes.

If your intermediate indirect references allow for null, then you need to determine yourself the order in which you need to perform the save and split the query into two subqueries.

For instance, if case needs a A which needs a B which needs a Case2TurbineUnitType, and A->B is nullable, then you can do:

def A def case case->A def Case2TurbineUnitType Case2TurbineUnitType->case def B B->Case2TurbineUnitType

save A

A->B

save A

Still, circular references should generally be avoided if you can

Cheers