Telerik blogs


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.

BUILDING A FOUNDATION

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”.

clip_image002[4]

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.

clip_image004[4]

Build the project – now we have our Entity Framework data layer available for us to use.

GENERATING AN OBJECTCONTEXT AND USING THE ENTITYDATASOURCE

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. 

clip_image005[4]

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.

 

clip_image006[4]

Select Online and search for “EF 5.x EntityObject Generator for C#” and add it to your project.

clip_image008[4]

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.

clip_image010[4]

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.

clip_image011[4]

When choosing the ObjectContext member, scroll to the bottom of the list and select the Products ObjectSet.

clip_image012[4]

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.

clip_image013[4]

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.

clip_image015[4]

 

USING ENTITY FRAMEWORK WITH TELERIK REPORT DESIGNER

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 -->

<!--

    <Telerik.Reporting>

       <AssemblyReferences>

              <add name="MyFunctions" version="1.0.0.0" />

       </AssemblyReferences>

    </Telerik.Reporting>

 -->

  <Telerik.Reporting>

    <AssemblyReferences>

      <add name="EntityFrameworkWithReporting.Data" version="1.0.0.0"

           culture="neutral" publicKeyToken="null" />

    </AssemblyReferences>

  </Telerik.Reporting>

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.

clip_image016[4]

In the next screen you will see that the ObjectContext from our data assembly is available to us to select.

clip_image017[4]

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.

clip_image019[4]

CONCLUSION

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.

Download Telerik Reporting


About the Author

Carey Payette

Carey Payette is a Senior Software Engineer with Trillium Innovations (a Solliance partner), an ASPInsider, a Progress Ninja, a Microsoft Certified Trainer and a Microsoft Azure MVP. Her primary focus is cloud integration and deployment for the web, mobile, big data, AI, machine learning and IoT spaces. Always eager to learn, she regularly tinkers with various sensors, microcontrollers, programming languages and frameworks. Carey is also a wife and mom to three fabulous boys.

Comments

Comments are disabled in preview mode.