LLBLGen Version: 2.6
Template: adapter
Here are my tables and their relationships:
One Application to many CheckList (cascade delete)
One CheckListType to many CheckList
One ApplicationType to many Application
One ApplicationType to many CheckListType
I suppose there is a circular path going on:
Application -> CheckList -> CheckListType -> ApplicationType -> Application
Below is the code that fails. The code is a test I wrote that recreates the steps of a scenario that is happening in our real software. It encounters the same error as our real software. This scenario in our real software worked using LLBLGen 1.x but no longer works now that we are using LLBLGen 2.6.
private void button2_Click(object sender, EventArgs e)
{
DataAccessAdapter adapter = new DataAccessAdapter();
// get application type
ApplicationTypeEntity appType = new ApplicationTypeEntity();
appType.Id = 2; // zoning
if (adapter.FetchEntity(appType))
{
// create application entity
ApplicationEntity app = new ApplicationEntity();
app.ApplicationType = appType;
app.Year = 2010;
// fill application's checklist
FillApplicationCheckList(ref app, ref appType, ref adapter);
// now clear it out.
app.CheckList.Clear();
// now refill application's checklist
FillApplicationCheckList(ref app, ref appType, ref adapter);
// save
bool refetchAfterSave = true;
bool recurse = true;
adapter.SaveEntity(app, refetchAfterSave, recurse);
}
adapter.CloseConnection();
}
void FillApplicationCheckList(ref ApplicationEntity app, ref DataAccessAdapter adapter)
{
// fill default checklist types collection for the application type
IRelationPredicateBucket checkListTypeBucket = app.ApplicationType.GetRelationInfoCheckListType();
checkListTypeBucket.PredicateExpression.Add(CheckListTypeFields.Default == true);
adapter.FetchEntityCollection(app.ApplicationType.CheckListType, checkListTypeBucket);
// fill application checklist collection by cycling through the
// default CheckListTypes and adding a CheckList for each one.
foreach (CheckListTypeEntity checkListType in app.ApplicationType.CheckListType)
{
CheckListEntity checkListItem = new CheckListEntity();
checkListItem.CheckListType = checkListType;
checkListItem.Complete = false;
app.CheckList.Add(checkListItem);
}
}
Here is the exception that happens when calling SaveEntity().
An exception was caught during the execution of an action query: Cannot insert the value NULL into column 'ApplicationID', table 'GCSLRMS_Buffalo_DB31.PT.CheckList'; column does not allow nulls. INSERT fails.
I have a feeling the problem has something to do with ApplicationType.CheckListType collection. Because in my second test below, I omit filling ApplicationType.CheckListType and just hardcode some values for CheckList.CheckListType instead. I get no error.
private void button3_Click(object sender, EventArgs e)
{
DataAccessAdapter adapter = new DataAccessAdapter();
// create application
ApplicationEntity app = new ApplicationEntity();
app.ApplicationTypeId = 2; // Zoning
app.Year = 2010;
// fill CheckList collection
CheckListEntity checkList = new CheckListEntity();
checkList.CheckListTypeId = 1;
checkList.Complete = false;
app.CheckList.Add(checkList);
// clear CheckList collection
app.CheckList.Clear();
// refill CheckList collection
app.CheckList.Add(checkList);
// save
bool refetchAfterSave = true;
bool recurse = true;
adapter.SaveEntity(app, refetchAfterSave, recurse);
adapter.CloseConnection();
}