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

[Solved] How to map a view?

3 Answers 205 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.
tmlipinski
Top achievements
Rank 1
tmlipinski asked on 24 Apr 2009, 02:02 PM
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

3 Answers, 1 is accepted

Sort by
0
Alexander
Telerik team
answered on 16 May 2009, 01:11 PM
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.
0
tmlipinski
Top achievements
Rank 1
answered on 17 May 2009, 09:08 PM
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
0
Alexander
Telerik team
answered on 19 May 2009, 08:14 AM
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.
Tags
General Discussions
Asked by
tmlipinski
Top achievements
Rank 1
Answers by
Alexander
Telerik team
tmlipinski
Top achievements
Rank 1
Share this question
or