All docs
V21.2
21.2
21.1
20.2
20.1
19.2
19.1
The page you are viewing does not exist in version 19.1. This link will take you to the root page.
18.2
The page you are viewing does not exist in version 18.2. This link will take you to the root page.
18.1
The page you are viewing does not exist in version 18.1. This link will take you to the root page.
17.2
The page you are viewing does not exist in version 17.2. This link will take you to the root page.

Entity Framework Data Source in ASP.NET Web Forms

  • 4 minutes to read

This topic shows how to add the DashboardEFDataSource to an in-memory data source storage, and make it available to users.

DashboardEFDataSource supports the following Entity Framework versions:

  • Entity Framework 5.0 and higher.
  • Entity Framework Core 1.0 and higher.

Specify a Database Connection

Specify a connection to the database in Web.config.

In this example, the connection name is OrdersContext. The connection supplies the dashboard with data from the MS SQL Server database file (NWind.mdf). You can find this database in the C:\Users\Public\Documents\DevExpress Demos 21.2\Components\Data directory.

<configuration>
  <connectionStrings>
    <add name="OrdersContext" connectionString="data source=(LocalDB)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\NWind.mdf;integrated security=True;connect timeout=30;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
</configuration>

The Web Dashboard supports the following data providers: Custom Connection Strings for Data Sources.

Create an ADO.NET Entity Data Model

  1. Right-click the project and select Add → New Item…. In the opened dialog, select ADO.NET Entity Data Model.

  2. Specify OrdersContext as the model name and select the Code First from database model type in the invoked wizard.

  3. Select the added OrdersContext string as a data connection for the created model.

  4. On the next page, specify which tables and views to include in your model.

Note that the Entity Framework context class should be public.

In this tutorial, the data model is based on the OrderDetail table. The code below shows the generated data model.

C#:

namespace WebFormsDashboardDataSources {
    using System.Data.Entity;

    public partial class OrdersContext: DbContext {
        public OrdersContext()
            : base("name=OrdersContext") {
        }

        public virtual DbSet<OrderDetail> OrderDetails { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder) {
            modelBuilder.Entity<OrderDetail>()
                .Property(e => e.UnitPrice)
                .HasPrecision(10, 4);
        }
    }
}

Visual Basic:

Imports System.Data.Entity

Namespace WebFormsDashboardDataSources

    Public Partial Class OrdersContext
        Inherits DbContext

        Public Sub New()
            MyBase.New("name=OrdersContext")
        End Sub

        Public Overridable Property OrderDetails As DbSet(Of OrderDetail)

        Protected Overrides Sub OnModelCreating(ByVal modelBuilder As DbModelBuilder)
            modelBuilder.Entity(Of OrderDetail)().Property(Function(e) e.UnitPrice).HasPrecision(10, 4)
        End Sub
    End Class
End Namespace

Register an Entity Framework Data Source

For example, your ASPX page contains the ASPxDashboard control which unique identifier is ASPxDashboardEf:

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div style="position: absolute; top: 0; bottom: 0; left: 0; right: 0">
            <dx:ASPxDashboard ID="ASPxDashboardEf" runat="server" Width="100%" Height="100%">
            </dx:ASPxDashboard>
        </div>
    </form>
</body>
</html>

You can define the Entity Framework Data Source in the code-behind page that has the .aspx.cs or .aspx.vb extension depending on the language used:

Note

A code-behind page is one of the variants where you can register the data sources. For example, you can also register them in the Global.asax.cs (Global.asax.vb) file.

using System;
using System.Web.Hosting;
using DevExpress.DashboardCommon;
using DevExpress.DashboardWeb;
using DevExpress.DataAccess.EntityFramework;

namespace WebFormsDashboardDataSources.Pages {
    public partial class EFDashboard : System.Web.UI.Page {
        protected void Page_Load(object sender, EventArgs e) {
        // ...
            // Create a data source storage.
            DataSourceInMemoryStorage dataSourceStorage = new DataSourceInMemoryStorage();

            // Register an Entity Framework data source.
            DashboardEFDataSource efDataSource = new DashboardEFDataSource("EF Core Data Source");
            efDataSource.ConnectionParameters = new EFConnectionParameters(typeof(OrdersContext));
            dataSourceStorage.RegisterDataSource("efDataSource", efDataSource.SaveToXml());

            // Set the configured data source storage.
            ASPxDashboardEf.SetDataSourceStorage(dataSourceStorage);

            ASPxDashboardEf.InitialDashboardId = "dashboardEf";
        }
    }
}

The EF Core Data Source is now available in the Web Dashboard:

web-dashboard-ex-core-entity-framework-core-data-source

Users can now bind dashboard items to data in the Web Dashboard’s UI.

Example

The example shows how to make a set of data sources available for users in the Web Dashboard application.

web-dashboard-a-list-of-data-sources

View Example: How to Register Data Sources for ASP.NET Web Forms Dashboard Control