One to One Mapping

2 posts, 0 answers
  1. Jaime Weise
    Jaime Weise avatar
    25 posts
    Member since:
    Nov 2008

    Posted 26 Jun 2010 Link to this post

    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?


    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?

  2. Petko_I
    Petko_I avatar
    184 posts

    Posted 28 Jun 2010 Link to this post

    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):

    After this addition your configuration should look like this:

          <backendconfiguration id="mssqlConfiguration" backend="mssql">

    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;
     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:

    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,
    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
  3. DevCraft banner
Back to Top