Change table mapping of entity at runtime

2 posts, 0 answers
  1. Simon Störmer
    Simon Störmer avatar
    31 posts
    Member since:
    Oct 2012

    Posted 01 Feb 2010 Link to this post


    I am completely new to OpenAccess. I have searched the forum for a while but have not found what I was looking for. If my questions have been already answered, please point me to the right thread.

    lets assume I have two tables in my database, that have an identical structure. Is it possible to tell OpenAccess which of both tables to use prior to an CRUD-Operation? And how can OA handle tables that have no primary key defined? Can I hand in a colum that should be treated as a PK?

    Thank you and greetings,

    Tobias Richling.
  2. Damyan Bogoev
    Damyan Bogoev avatar
    581 posts

    Posted 04 Feb 2010 Link to this post

    Hi Tobias Richling,

    Unfortunately this is not possible just "out of the box" but there is an approach that you can use to achieve similar functionality. For this purpose you can use a hierarchy of classes - a base class containing all necessary fields and two derived classes (without additional fields) which will be mapped to two different tables. In the Forward mapping wizard you should set the Inheritance strategy of the base class to "horizontal". When this option is set, OpenAccess will not create a table for the base class but each derived class will have its own table. Those tables will be identical as long as you do not have additional fields in the derived classes.

     Let’s consider the following sample model:

    public class BaseClass
        //private fields
    public class DerivedClassOne : BaseClass
    public class DerivedClassTwo : BaseClass

    You could use the following cast method in order to cast the base class object to the right child class:

    private static T CastObject<T>(BaseClass bc) where T : class
        if (typeof(T) == typeof(DerivedClassOne))
            return new DerivedClassOne { /*Initialize the DerivedClassOne object properties*/ } as T;
        else if (typeof(T) == typeof(DerivedClassTwo))
            return new DerivedClassTwo { /*Initialize the DerivedClassTwo object properties*/ } as T;
        return null;

    Inside the CRUD method you can do the following:

    BaseClass bc = new BaseClass { /*Initialize the BaseClass object properties*/ };
    DerivedClassTwo dco = CastObject<DerivedClassTwo>(bc);

    Regarding your second question, it is possible to reverse map tables without primary keys but you will have to manually set an ID field for each class. This can be done in the Reverse mapping wizard while mapping the tables. Note that it is your responsibility to choose an ID field which is mapped to a column with unique values. Otherwise you could observe side effects while working with the objects.
    Hope that helps.

    Damyan Bogoev
    the Telerik team

    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
    Follow the status of features or bugs in PITS and vote for them to affect their priority.
  3. DevCraft banner
Back to Top