Binding to relations with nullable FKs

Posts   
 
    
stevenpack
User
Posts: 4
Joined: 05-Mar-2007
# Posted on: 09-Mar-2007 12:57:31   

Hi All,

I'm building a data entry/maintenance screen. My database tables are as such:

Company (nullable FK to Address) Address Contact (nullable FK to Company)

they are mapped on the Entities as

Company with Company.Address and Company.Contacts Address Contacts

The foreign keys to both of those tables are nullable. This is to represent the fact that a user does not have to enter all of the information at one time. This, of course presents a problem when binding the Company object to controls on the form.


textBox1.DataBindings.Add("Text", companyBindingSource, "CompanyName");
textBox2.DataBindings.Add("Text", companyBindingSource, "Address.AddressLine1");

Address will often be null.

So, what are my options here? I could remove the nullability on the FKs, so there will always be an Address. I don't like the idea of modifying my schema to meet the needs of the UI though.

I could create a custom property on Company like AddressForDataBinding and provide an instance for databinding.

I could also explicitly instantiate an Address object on retrieving a Company if an Address has not been defined, but that will mean it will get persisted even if nothing is entered, which is just like removing the non-nullability, but at the app layer instead of the db layer.

I could also (and probably will) map Address fields to the Company entity. As in:

Company.AddressLine1 <-- Company.Address.AddressLine1

Not sure about contacts yet.

Interested in thoughts and opinions.

Steve.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 09-Mar-2007 14:59:11   

By default fields mapped on related entities return new empty entity/collection if not found on the database.

Are you using SelfServicing and Adapter?

Following is quoted from the LLBLGen Pro manual: "Using the gGenerated code -> SelfServicing -> Using the entity classes"

When the related entity is not found in the database, for example Customer has an optional relation (weak relation) with Address using Customer.VisitingAddressID - Address.AddressID and myCustomer.VisitingAddress is accessed and myCustomer doesn't have a related visiting address entity, by default the generated code will return a new, empty entity, in this case a new AddressEntity instance. You can then test the Fields.State value of the returned entity, if it is a new entity or a fetched entity (by comparing the Fields.State property with EntityState.New for a new entity or EntityState.Fetched for a fetched entity).

This can be cumbersome in some situations. You can tell the entity to return null (C#) or Nothing (VB.NET) instead of a new entity if the entity is not found by setting the property FieldMappedOnRelationReturnNewIfNotFound to false. In our example of the Customer and its optional VisitingAddress field, mapped on the relation Customer.VisitingAddressID - Address.AddressID, Customer will have a property VisitingAddressReturnNewIfNotFound. Setting this property to false will make myCustomer.VisitingAddress return null (C#) or Nothing (VB.NET) if the related Address entity is not found for myCustomer. By default these flags are set to true, to avoid code breakage with existing code already in production. You can change this default in the LLBLGen Pro designer: in the preferences and project properties, change the preference (which is inherited by a new project) or project property (if you're working on an existing project, be sure you set the property on the project as well) LazyLoadingWithoutResultReturnsNew to false and re-generate your code. The code generator will now generate 'false' / False' for all FieldMappedOnRelationReturnNewIfNotFound flags in all entities which will make sure that if an entity doesn't exist, null / Nothing is returned instead of a new entity.