Using the entity objects which relate to other objects

Posts   
 
    
KastroNYC
User
Posts: 96
Joined: 23-Jan-2006
# Posted on: 24-Jan-2006 09:21:14   

Hello,

I've got an entity named Employee with a field ContactID which references a ContactInformation entity. When I try to use the generated llblgen code like this i get an error, is there a better way to do this?

VB.NET

Dim emp As New EmployeeEntity() emp.ContactInformation.FirstName = "First" emp.ContactInformation.LastName = "Last" emp.PrivatePhoneNumber = "(123)456-7890" emp.Save()

I get an error stating that Employees.ContactID can not be NULL however employees.contactid is a reference to the PK of ContactInformation. How does LLBLGen automatically take care of this type of relationship. For clarity I list a basic table definition below:

Employee (Table)

EmployeeID PK ContactID FK PrivatePhoneNumber

ContactInformation (Table)

ContactID PK FirstName LastName

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 24-Jan-2006 14:07:55   

Please check the LLBLGen Pro documentation and read the section FK-PK synchronization under "Using the generated code - Adapter/Selfservicing - Using the entity classes"

KastroNYC
User
Posts: 96
Joined: 23-Jan-2006
# Posted on: 25-Jan-2006 05:19:48   

Hello I've refered to the help as you suggested:

FK-PK synchronization _ This synchronization of FK-PK values is already done at the moment you set a property to a reference of an entity object, for example myOrder.Customer = myCustomer, if the entity (in this case myCustomer) is not new. Synchronization is also performed after a save action, so identity/sequenced columns are also synchronized_

However i'm getting the following error:

An exception was caught during the execution of an action query: Cannot insert the value NULL into column 'ContactID', table 'devre.dbo.Employee'; 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.

The code I am trying to use looks like this:

    Dim emp As New EmployeeEntity()
    Dim con As New ContactInformationEntity()

    con.FirstName = "First"
    con.LastName = "Last"
    con.BuildingNumber = "123"
    con.StreetName = "Fake"
    con.StreetSuffix = "Street"
    con.City = "CityName"
    con.State = "ST"
    con.ZipCode = "12345"
    con.ZipCodePlusFour = "1234"
    emp.ContactInformation = con

    emp.LicenseNumber = "ABCDE"
    emp.DateStarted = DateTime.Now

    emp.Save()

My tables resemble this:

Employee

EmployeeID PK ContactID FK LicenseNumber DateStarted

ContactInformation

ContactID PK FirstName LastName etc.

Should I be creating the ContactInformation and saving it seperately then when I want to insert into Employee do: emp.ContactID = con.ContactID?

This software seems like it could save me alot of time, if I can figure out how to use it! Thanks very much for your help I really appreciate it.

-Kas

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 25-Jan-2006 06:21:29   

Please try

emp.Save(true)

The following was coppied from the LLBLGen Pro documentation:

In SelfServicing, this logic is not enabled by default, to be backwards compatible. You have to call the Save() (entities) or SaveMulti() (entity collections) overloads which accept a boolean parameter to signal the routine to save all entities recursively or not. Pass true to the Save / SaveMulti call and the whole object graph is saved

KastroNYC
User
Posts: 96
Joined: 23-Jan-2006
# Posted on: 25-Jan-2006 07:22:30   

I'm sorry for the silly question brother, couldn't seem to follow the documentation properly. Thanks a million. -Kas