This is a migrated thread and some comments may be shown as answers.

One to One Mapping

1 Answer 112 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Jaime Weise
Top achievements
Rank 1
Jaime Weise asked on 26 Jun 2010, 09:00 AM

public class Person 
    public Int32 ID { get; set; } 
    public Int32 FavoriteColorID { get; set; } 
    public Color FavoriteColor { get; set; }  
 
public class Color 
    public Int32 ID { get; set; } 
    public String ColorName { get; set; } 
 

Maybe this should be titles many to one, where there could be many people referencing shared color objects.

Say I have a Person object and a Color object.

Usually, I just add a foreign key constraint from the FavoriteColorID property to the ID of the Color table. Since, I am experimenting with Open Access Forward Mapping, how do I create this relationship with these two classes so that I can access the person's favorite color in the following syntax?

myPersonObject.FavoriteColor.ColorName 

I am also assuming that this related object would be eagerly loaded when we retrieved myPersonObject from the database. I am not clear if my strategy is correct but I am used to it with Entities Framework.

I can see two scenarios for using the FavoriteColor field; the first is when I am updating or creating the person's favorite color; the second is when I am displaying the person's favorite color in statically. In the latter case it seems to me, that you should always have that color available in the Person object (ie loaded eagerly) and stored in the FavoriteColor property. I could use a method GetFavoriteColorbyID everytime I needed that particular Color object or any of its properties but this seems like an extra database trip.

Is there an implementation where I can access the eagerly loaded FavoriteColor/(Color object)? This is straight forward in the Entities Framework. Does this work with OpenAccess using Forward Mapping?

Jaime 

1 Answer, 1 is accepted

Sort by
0
Petko_I
Telerik team
answered on 28 Jun 2010, 06:37 PM
Hello jaime,

You must make sure that the FavoriteColorID and FavoriteColor fields are mapped to the same column in order to have synchronization in the foreign key references.
However, there are some additional steps you need to take to achieve your goals prior to that. Firstly, you must specify that the two classes are persistent capable by either using the Forward Mapping Wizard or manually marking the classes with an attribute Telerik.OpenAccess.Persistent.

The OpenAccess  forward mapping will handle the foreign key relationships automatically but you need to specify the option in the application configuration file. The foreign key constraints can be generated by adding the following tag to your application configuration file (App.Config):


<dbNullForeignKey>true</dbNullForeignKey>
After this addition your configuration should look like this:


<backendconfigurations>
      <backendconfiguration id="mssqlConfiguration" backend="mssql">
        <dbNullForeignKey>true</dbNullForeignKey>
        <mappingname>mssqlMapping</mappingname>
      </backendconfiguration>
</backendconfigurations>

In order to update the database constraints you can go to Telerik -> OpenAccess -> Database Operations and click Update Database or rebuild your project.

You should be careful when you define properties without explicitly providing private fields that correspond to them. In order to take full advantage of all the features OpenAccess provides you need to specify the properties in the following way:

private int id;
 
[Telerik.OpenAccess.FieldAlias("id")]
 public Int32 ID
 {
     get { return id; }
     set { id = value; }
 }
The alias provides hints to OpenAccess which are used to establish connections between properties and fields. These connections are important in your case. To eagerly load related items you can use our Fetch Plans feature. It allows you to specify properties that should be loaded together whenever you retrieve data from the database. To be able to expose the fields which should be loaded together, you need the FieldAlias attribute.  You can browse our online documentation for understanding exactly how to use the Fetch Plans:

http://www.telerik.com/help/openaccess-orm/openaccess-tasks-howto-load-related-objects.html


Hope this information will help you to quickly develop the scenario you desire. Do not hesitate to contact us back if you have further questions.

All the best,
Petko_I
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
General Discussions
Asked by
Jaime Weise
Top achievements
Rank 1
Answers by
Petko_I
Telerik team
Share this question
or