Tip:
Highlight text to annotate it
X
In this video, we’ll go over the steps on how to databind a report using Entity Framework.
First thing we need to do is add the Data. I right-click on the project and choose add
new item. Select the Data tab and choose the ADO.NET Entity Data Model. I’ll call it
Northwind and hit add. I want to generate the model from a database So I click Next
and here I’ll make a new connection. I’ll bind to the local server , select the Northwind
database, and click OK. I’ll save the Enity Connection as NorthwindEntities
and click Next. Now, I’ll select the Categories table and Click Finish to complete the wizard.
Next, we need to add a new report. I’ll right-click and add a new item. Under Reporting,
choose the DXperience Report Class, give it a name, and click OK. Now, I’ll go to the
Solution Explorer and find the Category class under Northwind. You’ll see here that it’s
a plain CLR object with no database specific information or logic for pulling data.
So first I’ll compile the project, then o use this class, we have to go tot the Report’s
smart tag and add a new project data source. The Data configuration wizard appears. Here
I’ll chose Object and click next. Now, drill down into Reports , choose the Category Table,
and click finish. Once completed, you’ll see all available fields show up in the Field
List. Next, I’ll drop the CategoryName field onto
the design surface and resize it. You’ll notice that when I go to preview the report,
there’s no data shown. This is because the category class has no logic for accessing
data. In our dataset example, table adapters were automatically built in to provide the
mechanism for accessing data. I’ll show you how to fix this using scripting.
First, we need to tell our scripting engine where Entity Framework lives. So, I’ll scroll
down to the ScriptReferences property and click on the ellipsis. A script editor appears
and here I can add a path to Entity Framework. This is located in the packages folder inside
of Entity Framework because it used Nugit to pull the assembly. I click OK to complete
the editor. Now we need to add the data logic via a special
event. I’ll create a new event for the OnDataSourceDemanded script. Here, I’ll make sure to include
the appropriate using statements. Next, I’ll set the datasource of the report using datasource
and create a new Northwind Entities class, select the Categories table, and materialize
it using the ToList feature. Lastly, we need to add a reference to the
Windows Form assembly and hit OK. Now when we go to Preview the report, you’ll see
Our list of Category Names appear. And that’s how you databind to Entity Framework 5. Thanks
for watching and thanks for choosing devexpress.