All docs
V20.2
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

  • 4 minutes to read

This tutorial shows how to add the DashboardEFDataSource to a data source storage and make it available to users. The tutorial uses the MDF database.

  1. In your application, add the NWind.mdf database to the App_Data folder from the C:\Users\Public\Documents\DevExpress Demos 20.2\Components\Data directory.

  2. In Web.config, specify a connection string to the database.

    <configuration>
        <connectionStrings>
            <add name="NWindConnectionString" connectionString="data source=(localdb)\mssqllocaldb;attachdbfilename=|DataDirectory|\NWind.mdf;integrated security=True;connect timeout=120" providerName="System.Data.SqlClient" />        
        </connectionStrings>
    </configuration>
    
  3. Right-click the project and select Add | ADO.NET Entity Data Model. Specify OrdersContext as the model name and select the Code First from database model type in the invoked wizard.
  4. Select the added NWindConnectionString as a data connection for the created model.
  5. On the next page, specify which tables and view to include in your model. 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
    
        Partial Public Class OrdersContext
            Inherits DbContext
            Public Sub New()
                MyBase.New("name=OrdersContext")
            End Sub
    
            Private privateOrderDetails As DbSet(Of OrderDetail)
            Public Overridable Property OrderDetails() As DbSet(Of OrderDetail)
                Get
                    Return privateOrderDetails
                End Get
                Set(ByVal value As DbSet(Of OrderDetail))
                    privateOrderDetails = value
                End Set
            End Property
    
            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
    
  6. In the Default.aspx.cs (or .vb) file, create a public method that returns the configured dashboard's data source storage (DataSourceInMemoryStorage) and define the EF data source.

    using System;
    using DevExpress.DashboardCommon;
    using DevExpress.DashboardWeb;
    using DevExpress.DataAccess.EntityFramework;
    
    public DataSourceInMemoryStorage CreateDataSourceStorage() {
        DataSourceInMemoryStorage dataSourceStorage = new DataSourceInMemoryStorage();
    
        DashboardEFDataSource efDataSource = new DashboardEFDataSource("EF Core Data Source");
        efDataSource.ConnectionParameters = new EFConnectionParameters(typeof(OrdersContext));
        dataSourceStorage.RegisterDataSource("efDataSource", efDataSource.SaveToXml());
    
        return dataSourceStorage;
    }
    
  7. Call the ASPxDashboard.SetDataSourceStorage method to configure the data source storage. Use the created CreateDataSourceStorage method as the SetDataSourceStorage parameter.

    using DevExpress.DashboardWeb;
    
    protected void Page_Load(object sender, EventArgs e) {
        // ...  
    
        // Configures the data source storage.
        ASPxDashboard1.SetDataSourceStorage(CreateDataSourceStorage());
    }
    

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

web-dashboard-ex-core-ef-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.

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