Mapping references and collections in Telerik OpenAccess ORM (Part 1)

Posted by: the telerik blogs, on 26 Oct 2009 | View original | Bookmarked: 1 time(s)

The references in the object model and the references in the database schema can be defined in several ways. The definitions are following different criteria, so it is not quite straight forward how a mapping is to 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, one as a reference from Person to Address, and another inside the Persons collection. This makes it necessary to define a master, which 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 automatically does this for you. 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 adding Person objects to the collection.

3. The third way is to expose the collection only, but not the reference. This cannot be achieved with the reverse engineering wizard. In a forward case you open the forward mapping wizard, and 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 and leave your feedback!

Advertisement
Free Agile Project Management Tool from Telerik
TeamPulse Community Edition helps your team effectively capture requirements, manage project plans, assign and track work, and most importantly, be continually connected with each other.
Category: Data | Other Posts: View all posts by this blogger | Report as irrelevant | View bloggers stats | Views: 2194 | Hits: 14

Similar Posts

  • Using WCF with SQL Azure and Telerik OpenAccess more
  • Connecting to SQL Azure with Telerik OpenAccess more
  • How to display data from different tables using one data source more
  • Telerik OpenAccess WCF Wizard October CTP more
  • Binding Hierarchical RadGrid for ASP.NET Ajax with Telerik OpenAccess ORM more
  • Binding Hierarchical RadGrid with Telerik OpenAccess ORM more
  • Using the Telerik OpenAccess WCF Wizard with Multiple Versions of OpenAccess more
  • Reverse Mapping and Using Database Views with Telerik OpenAccess ORM more
  • Telerik Introduces Free Web Testing Framework for ASP.NET AJAX and Silverlight more
  • How to Enhance Projects Using MSBuild on a Machine Without Telerik OpenAccess ORM Installed more

News Categories

.NET | Agile | Ajax | Architecture | ASP.NET | BizTalk | C# | Certification | Data | DataGrid | DataSet | Debugger | DotNetNuke | Events | GridView | IIS | Indigo | JavaScript | Mobile | Mono | Patterns and Practices | Performance | Podcast | Refactor | Regex | Security | Sharepoint | Silverlight | Smart Client Applications | Software | SQL | VB.NET | Visual Studio | W3 | WCF | WinFx | WPF | WSE | XAML | XLinq | XML | XSD