1:1 Relationship Problem - SelfService Code

Posts   
 
    
jshallard
User
Posts: 62
Joined: 23-Mar-2005
# Posted on: 06-Sep-2006 15:26:19   

We have two tables in a database that have a 1:1 relationship set up in the following way:

  • Table1 has a Primary Key which is an Identity column
  • Table2 has a Primary Key which is a ForeignKey of Table1

This means that the primary key of Table1 & Table2 will always be the same value. The SelfService generated code seems to correctly set up a 1:1 relationship within the code.

The Problem When trying to create a new Table2 entity item within the code, we set the PrimaryKey to be the same as the PrimaryKey in Table1. When saving we get an error message saying that Null can not be inserted into the ID field. Through debugging we found that despite setting the ID field value, it was missing from the insert statement.

Any help appreciated

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 06-Sep-2006 15:42:02   

Would you please post the corresponding code.

jshallard
User
Posts: 62
Joined: 23-Mar-2005
# Posted on: 06-Sep-2006 15:57:04   

Hi Walaa

Thanks for your prompt response. The tables/entities with a 1:1 relationship are Case and Client. I can post the SQL table generate scripts if that would help, but for now here is the code that is throwing the error:


// Create new case
CaseEntity caseEntity = new CaseEntity();
...
caseEntity.Save();

// Create new client
caseEntity.Client   = new ClientEntity();
caseEntity.Client.PkFkId  = caseEntity.PkId;
...
caseEntity.Client.Save();

Thanks for you help

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 06-Sep-2006 16:08:09   

The PK-FK are automatically set when you assign entities to each other. try the following code:


// Create new case
CaseEntity caseEntity = new CaseEntity();
... ... //set some fields fro the case entity
caseEntity.Client   = new ClientEntity();
... //set some fields fro the client entity

caseEntity.Save(true);  // the true, for recursive save.

(edit) Please refer to the LLBLGen Pro manual, look for "Saving entities recursively" & "FK-PK synchronization" under "Using the generated code -> SelfServicing -> Using the entity classes"

jshallard
User
Posts: 62
Joined: 23-Mar-2005
# Posted on: 06-Sep-2006 17:45:23   

Thanks for you help.