csmac3144 wrote:
I have a simple entity (Question), which has a 1:1 relation with anothher entity (Answer).
If I create and save an "Answer", then set Question.Answer to the saved entity, then adapter.SaveEntity(question) works.
If I create a new Answer (not yet saved) then assign that to the Question.Answer property and try to save I get an error. I though recursive saves would handle this, but maybe I misunderstood. Am I required to save entities myself before associating them with another new entity?
Recursive saves work similar to normal saves: the order is determined, and each entity is saved individually. So I don't understand why saving an Answer works, but doing it recursively doesn't.
The code is very simple:
[TestMethod]
public void BasicSaveQuestionTest()
{
QuestionBaseEntity questionBaseEntity = new QuestionBaseEntity();
questionBaseEntity.Name = "MyTest";
AnswerBaseEntity answerBaseEntity = new AnswerBaseEntity();
answerBaseEntity.Value = new byte[1];
questionBaseEntity.Answer = answerBaseEntity;
DataAccessAdapter adapter = new DataAccessAdapter();
adapter.SaveEntity(questionBaseEntity);
}
here is the error:
Test method SerpentTests.SurveyRepositoryTest.BasicSaveQuestionTest threw exception: SD.LLBLGen.Pro.ORMSupportClasses.ORMQueryExecutionException: An exception was caught during the execution of an action query: Cannot insert the value NULL into column 'AnswerBaseId', table 'Serpent.dbo.QuestionBase'; column does not allow nulls. INSERT fails.
The statement has been terminated.. Check InnerException, QueryExecuted and Parameters of this exception to examine the cause of this exception. ---> System.Data.SqlClient.SqlException: Cannot insert the value NULL into column 'AnswerBaseId', table 'Serpent.dbo.QuestionBase'; column does not allow nulls. INSERT fails.
The statement has been terminated..
Is the Answer you save here, the same as when you save it individualy? In other words: is answerBaseEntity.IsDirty true before the save in the code above? If not, answerBaseEntity isn't saved. If it's not saved, the PK value of answer isn't synced with the FK field in question. So the field in question then doesn't become 'set' and NULL is inserted into the field in Question, which results in the error.
Also, is the ID field in answer an identity field? Are you sure it is set as an identity field in the database?
I had a lot of other problems with LLBLgen recently (crashes, etc. detailed in another post here) so now I'm not sure what problems are my fault, and what might be due to a corrupted model or something like that.
I answered that question in the other thread so I think you haven't read that response, as you then would know it's not a corrupt project.