Telerik Reporting R1 2017

Connecting to an Entity Data Model with the EntityDataSource component

This section discusses how to connect the EntityDataSource component to an Entity Data Model in the case of Database First or Model First or directly to the DbContext inheritor in the case of Code First approach. The provided examples and code snippets assume an existing Entity Data Model of the Adventure Works sample database with the following structure:

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

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

var entityDataSource = new Telerik.Reporting.EntityDataSource();

entityDataSource.Context = typeof(AdventureWorksEntities);
entityDataSource.ContextMember = "Products";

var report = new Report1();

report.DataSource = entityDataSource;

The above code snippet connects the EntityDataSource 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 ObjectContext/DbContext. In this case however it is your responsibility to destroy that ObjectContext/DbContext instance when done with the report:

var entityDataSource = new Telerik.Reporting.EntityDataSource();
var context = new AdventureWorksEntities();

entityDataSource.Context = context;
entityDataSource.ContextMember = "Products";

var report = new Report1();

report.DataSource = entityDataSource;

// You have to dispose the Context explicitly when done with the report.
context.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 EntityDataSource component allows you to pass parameters to those arguments via the Parameters collection. For example, let us 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();
    }
}

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

var entityDataSource = new Telerik.Reporting.EntityDataSource();

entityDataSource.Context = typeof(AdventureWorksEntities);
entityDataSource.ContextMember = "GetProducts";
entityDataSource.Parameters.Add("color", typeof(string), "Black");
entityDataSource.Parameters.Add("price", typeof(decimal), 100);

var report = new Report1();

report.DataSource = entityDataSource;

Note Note

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