How to: Connect to a Telerik® Data Access Domain Model

This article is relevant to entity models that utilize the deprecated Visual Studio integration of Telerik Data Access. The current documentation of the Data Access framework is available here.

This topic discusses how to connect the OpenAccessDataSource component to an Telerik Data Access Domain Model. The provided examples and code snippets assume an existing Telerik Data Access Domain Model of the Adventure Works sample database with the following structure:

The most simple way to configure OpenAccessDataSource in Report Designer is to use OpenAccessDataSource Wizard. That wizard is started automatically when you create a new OpenAccessDataSource, but you can invoke it manually at any time from the context menu associated with the data source by choosing "Configure":

For more information about the OpenAccessDataSource Wizard, refer to the How to: Use the OpenAccessDataSource Wizard topic.

To configure the OpenAccessDataSource component programmatically you need to specify at least an OpenAccessContext and a property or a method from that OpenAccessContext which is responsible for the data retrieval. Assign the type of the OpenAccessContext to the ObjectContext property of OpenAccessDataSource and the name of the desired member to the ObjectContextMember property, as shown in the following example:

Telerik.Reporting.OpenAccessDataSource openAccessDataSource = 
    new Telerik.Reporting.OpenAccessDataSource();
openAccessDataSource.ObjectContext = typeof(AdventureWorksEntities);
openAccessDataSource.ObjectContextMember = "Products";
Telerik.Reporting.Report report = new Telerik.Reporting.Report();
report.DataSource = openAccessDataSource;
Dim openAccessDataSource As Telerik.Reporting.OpenAccessDataSource = 
    New Telerik.Reporting.OpenAccessDataSource()
openAccessDataSource.ObjectContext = GetType(AdventureWorksEntities)
openAccessDataSource.ObjectContextMember = "Products"
Dim report As Telerik.Reporting.Report = New Telerik.Reporting.Report()
report.DataSource = openAccessDataSource

The above code snippet connects the OpenAccessDataSource component to the AdventureWorksEntities context and retrieves the information for all products from the Products auto-generated property.

Instead of specifying a type, you can assign a live instance of the OpenAccessContext. In this case, it is your responsibility to destroy the OpenAccessContext instance when done with the report:

Telerik.Reporting.OpenAccessDataSource openAccessDataSource = 
    new Telerik.Reporting.OpenAccessDataSource();
AdventureWorksEntities openAccessContext = new AdventureWorksEntities();
openAccessDataSource.ObjectContext = openAccessContext;
openAccessDataSource.ObjectContextMember = "Products";
Telerik.Reporting.Report report = new Telerik.Reporting.Report();
report.DataSource = openAccessDataSource;
// You have to dispose the OpenAccessContext explicitly when done with the report.
openAccessContext.Dispose();
Dim openAccessDataSource As Telerik.Reporting.OpenAccessDataSource = 
    New Telerik.Reporting.OpenAccessDataSource()
Dim openAccessContext As AdventureWorksEntities = New AdventureWorksEntities()
openAccessDataSource.ObjectContext = openAccessContext
openAccessDataSource.ObjectContextMember = "Products"
Dim report As Telerik.Reporting.Report = New Telerik.Reporting.Report()
report.DataSource = openAccessDataSource
' You have to dispose the OpenAccessContext explicitly when done with the report.
openAccessContext.Dispose()

Binding to a method is more flexible than binding to a property, because it is possible to execute some custom business logic when retrieving data for the report. If the specified method has arguments, the OpenAccessDataSource component allows you to pass parameters to those arguments via the Parameters collection. For example, let's extend the AdventureWorksEntities context using a partial class that defines the following method:

partial class AdventureWorksEntities
{
   public System.Collections.Generic.List<Product> GetProducts(string color, decimal price)
   {
       return this.Products.Where(product => product.Color == color && 
            product.ListPrice <= price).ToList();
   }
}
Partial Class AdventureWorksEntities
    Public Function GetProducts(ByVal color As String, ByVal price As Decimal) As System.Collections.Generic.List(Of Product)
        Return Me.Products.Where(Function(product) product.Color = color And 
            product.ListPrice <= price).ToList()
    End Function
End Class

You can bind the OpenAccessDataSource component to that method with the following code snippet:

Telerik.Reporting.OpenAccessDataSource openAccessDataSource = 
    new Telerik.Reporting.OpenAccessDataSource();
openAccessDataSource.ObjectContext = typeof(AdventureWorksEntities);
openAccessDataSource.ObjectContextMember = "GetProducts";
openAccessDataSource.Parameters.Add("color", typeof(string), "Black");
openAccessDataSource.Parameters.Add("price", typeof(decimal), 100);
Telerik.Reporting.Report report = new Telerik.Reporting.Report();
report.DataSource = openAccessDataSource;
Dim openAccessDataSource As Telerik.Reporting.OpenAccessDataSource = 
    New Telerik.Reporting.OpenAccessDataSource()
openAccessDataSource.ObjectContext = GetType(AdventureWorksEntities)
openAccessDataSource.ObjectContextMember = "GetProducts"
openAccessDataSource.Parameters.Add("color", GetType(String), "Black")
openAccessDataSource.Parameters.Add("price", GetType(Decimal), 100)
Dim report As Telerik.Reporting.Report = New Telerik.Reporting.Report()
report.DataSource = openAccessDataSource

The names and types of the parameters in the Parameters collection should match the names and types of the method arguments. In case this requirement is not fulfilled, the OpenAccessDataSource component will not be able to resolve or call the method correctly and will raise an exception at runtime.

See Also