How to map a view?

Thread is closed for posting
4 posts, 0 answers
  1. tmlipinski
    tmlipinski avatar
    131 posts
    Member since:
    Dec 2006

    Posted 24 Apr 2009 Link to this post

    Hi,

    I know there is no straight support for mapping database views (yet). I try to do it manually - but how to declare that there is no primary key in such a "table"?
    Let's assume that my view is named "MyView" and has 2 columns: integer "MyInt" and string "MyString" (and none of them is unique and no pair of them is unique). How would it look like in app.config and in MyView.cs?

    Regards
    Tomasz
  2. Alexander
    Admin
    Alexander avatar
    727 posts

    Posted 16 May 2009 Link to this post

    Hi tmlipinski,

    Let us assume that we have two similar databases - DataBase1 and DataBase2. The first one has a View named "Combinations" exposing two colums (MyInt, MyString) from other tables. In DataBase2 we have a table also named "Combinations" with the desired MyInt and MyString columns of the same type as those in the View. We will use that table to reverse map a class with MyInt and MyString fields. The important part here is that we should set one of these columns as primary key. Otherwise the generated class will not compile. In this case the primary key can be the MyInt column. When we have mapped the DataBase2 to classes, we can just change the database name in the App.config file to "DataBase1". After doing so we can use the Combination class to retrieve the data from the view in DataBase1. The fact that we have a field marked as primary key in that class does not prevent us from reading records that may have same values in that column. Hope that helps.

    All the best,
    Alexander
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. tmlipinski
    tmlipinski avatar
    131 posts
    Member since:
    Dec 2006

    Posted 17 May 2009 Link to this post

    Hi,
    OK, it works but with one important notice: this dummy declared primary key must be a unique key in this view. Otherwise we get something like this:
    - there are 3 records in the view (MyInt, MyString): (1, "A"), (1, "B"), (1, "C")
    - we ask for all records with MyInt = 1
    - we get 3 records in the result; but all of them are the same record - due to the caching mechanism, of course
    It's enough to declare a primary key containing both MyInt and MyString (as dummy as the previous one) to get the proper result.

    So: it works but I hope it is a temporary, very temporary :-) solution.

    Regards
    Tomasz
  4. Alexander
    Admin
    Alexander avatar
    727 posts

    Posted 19 May 2009 Link to this post

    Hi tmlipinski,

    Support for views is one of our targets for the near future but we cannot give a time-frame for that yet . We are going to prepare an article on that topic that customers can use as a know-how in the meanwhile.

    Sincerely yours,
    Alexander
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
Back to Top