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

Map property to a scalar-valued function or procedure

5 Answers 103 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.
Tevez
Top achievements
Rank 1
Tevez asked on 03 Aug 2011, 11:46 PM
Hi everyone,

I have another question, i'm working with the OA ORM and have mapped some tables. But now i want to add an extra property that is not mapped to a column but to a scalar-valued function.
So the SQL should look a bit like SELECT ColumnA, ColumnB, dbo.fn_get_calculated_value(ColumnC) As ColumnD FROM...

Before i used Hibernate and i could easily add a property that was mapped to a "formula" , but in OA i cannot see how i could do something similar to that..

I found out that i can map a stored procedure, and use that in a property in my non-generated partial class.. but this is not an option, it creates way to many round trips to the database.

Any ideas?

Best Regards,
Tevez

5 Answers, 1 is accepted

Sort by
0
Kosta Hristov
Telerik team
answered on 05 Aug 2011, 03:41 PM
Hello Tevez,

 Currently, there is no support for such feature in OpenAccess. A property cannot be mapped to a custom SQL query nor to a stored procedure directly.
Anyway, you should be able to create a property in a partial class, as you said, which utilizes a stored procedure directly. If you want to make only one call to the database, you can cache the value of the property using a nullable field. This will limit the "trips" to the database to only 1 in the first call. Please check the attached project where you can find an example. In this demo, a very simple stored procedure is called :

CREATE PROCEDURE [dbo].[ConsoleApp_Dog_GetDogsCount]
AS
SELECT 1

The OpenAccessContextBase.ExecuteScalar() method is used to executes the stored procedure and obtain the first column of the first row of the query resultset.

In order to run the project don't forget to :
1. Generate the appropriate table in the database
  * right click on the designer and select "Update database from model"
2. Create the stored procedure using the script above.
3. Edit the connection string located in the App.Config file to point to your local database.

I hope this will help you solve your problem. If you have any more questions, please do not hesitate to ask.

All the best,
Kosta
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>
0
Tevez
Top achievements
Rank 1
answered on 06 Aug 2011, 10:58 AM
Hi Kosta,

Your example look great.. but when i try to use .ExecuteScalar in my project, or when i try to open your project, i don't see the ExecuteScalar method at all!? . I only have .ExecuteStoredProcedure.

Am i missing something? Or are we talking about different versions?

Regards, Tevez
0
Kosta Hristov
Telerik team
answered on 10 Aug 2011, 04:20 PM
Hello Tevez,

The ExecuteScalar method is part of the new ADO API, which was introduced with our Q2 release (2011_2_713). Can you please check if there isn't any version mismatch in your project. Maybe the project in which you are trying to call the ExecuteScalar is still referencing our old Assemblies. The easiest way to fix this problem is to use the auto-upgrade tool available with OpenAccess. You can click on the Upgrade Reference menu item in our OpenAccess menu to active the tool (see the attached image).




Hope this solves the problem.

All the best,
Kosta
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

0
Tevez
Top achievements
Rank 1
answered on 17 Aug 2011, 08:52 PM
Hi Kosta,

Thanks for your tips. I've upgraded, and now this method is available. And it works like it should.

But, i'm still far from what my goal was; mapping a property to a scalar function.
I've found a complex workaround for that, but it would be really nice if this UDF mapping would be placed on the OpenAccess roadmap.
My old NHibernate app uses a bunch of those properties and it gives me really powerful possibilities!

Best Regards, Tevez
0
Damyan Bogoev
Telerik team
answered on 18 Aug 2011, 01:13 PM
Hi Tevez,

Firstly I want to thank you for the valuable input.
I am afraid that currently such feature is not on our to-do list but we will consider it for the future versions of the product.

All the best,
Damyan Bogoev
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

Tags
General Discussions
Asked by
Tevez
Top achievements
Rank 1
Answers by
Kosta Hristov
Telerik team
Tevez
Top achievements
Rank 1
Damyan Bogoev
Telerik team
Share this question
or