This is a migrated thread and some comments may be shown as answers.
Change table mapping of entity at runtime
1 Answer 43 Views
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Simon Störmer
Top achievements
Rank 1
Simon Störmer asked on 01 Feb 2010, 02:55 PM
Hi,

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.

1 Answer, 1 is accepted

Sort by
0
Damyan Bogoev
Telerik team
answered on 04 Feb 2010, 03:09 PM
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:

[Telerik.OpenAccess.Persistent()]
public class BaseClass
{
    //private fields
    //properties
}
  
[Telerik.OpenAccess.Persistent()]
public class DerivedClassOne : BaseClass
{
}
  
[Telerik.OpenAccess.Persistent()]
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);
scope.Transaction.Begin();
scope.Add(dco);
scope.Transaction.Commit();

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.

Greetings,
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.
Tags
Development (API, general questions)
Asked by
Simon Störmer
Top achievements
Rank 1
Answers by
Damyan Bogoev
Telerik team
Share this question
or