NOTE: This blog post pertains to Entity Framework v4.0 and above. Previous versions of Entity Framework exposed the ObjectContext automatically and this was used in conjunction with the EntityDataSource component in Telerik Reporting. Since Entity Framework 4.0 was released a different strategy must be used. This blog entry demonstrates the usage of Entity Framework version greater than or equal to version 4.0 with Telerik Reporting
Data can come from many places. A level of abstraction sitting above the database is a common practice in enterprise application development. Entity Framework (EF) is among the most popular solutions in this space giving developers the ability manipulate data as objects that have been automatically generated from the database (or vice-versa if code-first practices are used). Entity Framework shields developers from tedious data access, validation and object mapping tasks. EF also provides the ease and simplicity of LINQ to obtain data required for an application, or in this case a report.
It is recommended that you keep your Entity Framework models and repository classes in a separate assembly. To get started, create an empty Visual Studio Solution and name it “EntityFrameworkWithReporting”.
Next add a C# class library project to the solution and name it “EntityFrameworkWithReporting.Data”, then delete the Class1.cs that is generated with the project. Now we can add a new ADO.Net Entity Model to the data project named “AdventureWorks.edmx”. Generate the model from the AdventureWorks sample database. For this example, select only the Production.Products table to be included in your model.
Build the project – now we have our Entity Framework data layer available for us to use.
In order to use the EntityDataSource in Telerik Reporting, you must first expose the models ObjectContext. Since Entity Framework 4.0, this context is not readily available anymore. All is not lost, there is a simple way to modify the code artifacts generated by Entity Framework that will give you access to the ObjectContext. A downloadable T4 code template is available that will generate an ObjectContext for use with newer versions of EF.
To get started implementing an ObjectContext in your EF model, expand your AdventureWorks.edmx file and view the supporting files. Next you will delete all the files that have the extension *.tt.
Next, open the design surface for the AdventureWorks entity model, and right click in the whitespace of the designer, then select the “Add Code Generation Item” menu item.
Select Online and search for “EF 5.x EntityObject Generator for C#” and add it to your project.
You are now able to use the EntityDataSource component with your AdventureWorks entity model.
Build the project, and add a new Telerik Report to the Reports project, name it “EFWithObjectContext.cs”, then cancel out of the Report wizard and delete the header and footer section of the report.
Next drag an instance of the EntityDataSource to the design surface and select the existing AdventureWorksEntities data connection. In the next screen, select the AdventureWorksEntities object context from the Data project.
When choosing the ObjectContext member, scroll to the bottom of the list and select the Products ObjectSet.
Now that we have our EntityDataSource setup, we are ready to setup our table in our report. Drag an instance of the Table Wizard to the details section of the report, and select the entity data source that we just setup as the source for the table. Select some fields to display in the table, and click Finish.
In the designer, expand the table to fill the horizontal space of the report. We are now ready to build the project and preview the report. Using this approach the Report Preview is available directly in Visual Studio, simply click the Preview Tab.
The approach to using Entity Framework outlined in this blog post can be utilized in conjunction with the stand-alone Telerik Report Designer. For this example, I will demonstrate adding the data assembly reference to the Report Designer as well using EntityDataSource from within the tool.
The ability to include and use additional assemblies is a straight-forward process with Telerik Report Designer. Simply copy the Data assembly from our solution and place it in the same folder as the Telerik Report Designer executable (typically located in a path similar to the following: C:\Program Files (x86)\Telerik\Reporting Q2 2013\Report Designer).
In the same folder as the Telerik Report Designer executable, locate and edit the “Telerik.ReportDesigner.exe.config” file. Toward the bottom of the file, add an entry to include our Data assembly in the application. The entry should look similar to the following:
<!-- Add assembly references -->
<add name="MyFunctions" version="18.104.22.168" />
<add name="EntityFrameworkWithReporting.Data" version="22.214.171.124"
culture="neutral" publicKeyToken="null" />
Be sure to also copy the connection string from the App.config to this configuration file. After this is done, save the file and open Telerik Report Designer. Create a new report, name it “EntityFrameworkReport”. You can then exit out of the Wizard and delete the header and footer sections of the report.
Next click the Data tab, and select the Entity Data Source item. Utilize the AdventureWorksEntities connection string that we copied over from our Data project configuration file.
In the next screen you will see that the ObjectContext from our data assembly is available to us to select.
As before select the Products ObjectSet as the member, and click Finish. Select the Details section of the report and click on the Insert tab item. Add a Table Wizard to the design surface, utilizing the EntityDataSource that we just configured, and add fields to display in the table just as before. Extend the width of the table in the designer horizontally and preview your report.
This post walked you through using newer versions of Entity Framework in conjunction with Telerik Reporting using both Visual Studio and Telerik Report Designer. By using a downloadable T4 code template, we were able to generate an entity model that exposed the ObjectContext of the Entity Model which in turn allowed us to use the EntityDataSource in our report. This makes creating reports quickly and easily based on EF data. Exposing the data assembly to Telerik Report Designer allows you to share your EF data with your end users so they can create their own custom reports.
Carey Payette is a Developer Advocate. You can follow Carey on Twitter @careypayette or read her personal blog at www.codingbandit.com.