Skip to main content

Binding to EF Data Sources

  • 3 minutes to read

The Dashboard Designer allows you to connect to an Entity Framework data source defined within the current project from an external assembly that contains the required context.

Create a Data Source in the Data Source Wizard

To bind a dashboard to an Entity Framework data source from the current project, do the following:

Select a Data Source and Specify Data Context

Click New Data Source in the Data Source ribbon tab.

DataBinding_NewDataSource

On the first page of the invoked Data Source Wizard dialog, select Entity Framework and click Next.

DataSourceWizard_DataSourceType_EF

On the next page, select the required data context and click Next.

ChooseDataContextPage

Select Data from the Database

On the next page, specify a connection string used to establish a data connection. The following options are available:

Default Connection String

Choose the default connection string if it is specified in the application’s configuration file.

EF_SelectConnectionString

Custom Connection String

Specify a custom connection string in the connection string editor.

EF specify custom connection string

Predefined Connection String

Select an existing connection string available in the current project.

EF select available string

Add Stored Procedures (Optional)

Note

The approach described in this section requires that your project contains an .edmx file that defines a conceptual model. The Code-First approach to calling stored procedures is not supported by DashboardEFDataSource.

The next wizard page is available only if the current entity data model contains stored procedures. This page allows you to add stored procedures to the data source and configure their parameters.

EF_AddStoredProcedurePage

Click Add and select the required stored procedures to add to the data source.

EF_SelectStoredProcedures

Specify the Value passed as a stored procedure parameter. Click the Preview… button to preview data returned by the stored procedure call.

EF_ConfigureProcedureParameters

As an alternative, enable the Expression check box to invoke the Expression Editor dialog. In the dialog, you can specify the expression or select an existing dashboard parameter to use it as a stored procedure parameter.

Apply filter Criteria

On the last page, you can apply filter criteria to the resulting query. Click Finish to create the data source.

EF filter query

Result

The data source fields are displayed in the Data Source Browser:

EF data source in the Data Source Browser

Create a Data Source in Code

To create an Entity Framework data source in code, create an instance of the DashboardEFDataSource class and specify the following properties:

  1. Use the EFDataSource.ConnectionParameters property to specify the connection parameters. The EFConnectionParameters object returned by this property allows you to specify the type of an Entity Framework context used to connect to data, the connection string, or use the data context from the external assembly.
  2. Use the EFDataSource.Fill method to retrieve data from the data source.

Add the created DashboardEFDataSource object to the Dashboard.DataSources collection.

The following code snippet shows how to create an Entity Framework data source and add it to the collection of dashboard data sources:

using DevExpress.DashboardCommon;
using DevExpress.DataAccess.EntityFramework;
// ...
            Dashboard dashboard = new Dashboard();
            DashboardEFDataSource efDataSource = new DashboardEFDataSource();
            efDataSource.ConnectionParameters = new EFConnectionParameters(typeof(SalesPersonModel), "SalesPersonModel");
            efDataSource.Fill();
            dashboard.DataSources.Add(efDataSource);