Telerik blogs

The references in the object model and the references in the database schema can be defined in several ways. The definitions are following different idioms and it is not straight forward how a mapping can be defined. In the database schema you have the foreign key reference; in the object model you have single references, collections and dictionaries.

Let’s look at a simple schema in the database, the Persons table has a foreign key reference to the Addresses table.

clip_image002

There are now 3 ways to map this in OpenAccess.

1. You can map it to a reference field which is the default for forward and reverse mapping.

clip_image004

If you are using reverse engineering (database first) and the foreign keys are defined in the database you do not have to change any setting in the reverse engineering wizard. Same if you are using forward mapping (model first), you decorate your classes with the [Persistent] attribute and OpenAccess does everything else for you.

2. Now let’s change the mapping. Sometimes it makes sense to be able to navigate from the Address object to the Person object as well. To do so, in the reverse case you have to select the reference and enable the ‘Create one-to-many list’ checkbox.

clip_image006

The wizard now generates the reference in the Person class and the collection in the Address class.

clip_image008

In the forward case you first have to implement the collection in the address class, similar to the model above. Now open the forward mapping wizard, select the reference, switch off ‘Join table’ and select the field on the opposite side. If the field is not visible in the combo box it might have the wrong type or you have to click the reload button in the dialog.

clip_image010

The collection on the Address side is filled by a query base on the ADDRESS_ID column in the PERSONS table. We have a logical problem here. The content of the ADDRESS_ID column is now twice in memory, once as reference from Person to Address and once inside the Persons collection. This makes it necessary to define a master, who is responsible for updating the database values. In OpenAccess the master is always the reference. If you add a Person to the Persons collection you have to update the reference on the opposite site as well.

But OpenAccess comes with a collection feature that does it for you automatically. In the forward mapping wizard select he collection again and set "Manage Collection" to true.

clip_image012

Now if you add or remove a Person object from the Persons collection the reference is updated automatically. Be careful because this mechanism is only working if the objects are managed by the scope. Please call scope.Add() with the address object before you add Person objects to the collection.

3. The third way is to expose only the collection but not the reference. This cannot be achieved with the reverse engineering wizard. In forward case you open the forward mapping wizard, select the collection. Now you have to switch off join table and select the inverse field as {auto}.

clip_image014

Managed is not available because we have only one side and here the collection is the master. The model should not contain the Address reference.

clip_image016

Part 2 will show how to work with Join Tables – stay tuned.


Comments

Comments are disabled in preview mode.