Skip to main content

Entity Framework Data Source

  • 4 minutes to read

This tutorial shows how to add the DashboardEFDataSource to an in-memory data source storage, and make it available to users. The MDF database is used as a sample.

  1. In your application, add the NWind.mdf database to the App_Data folder from the C:\Users\Public\Documents\DevExpress Demos 24.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. Set OrdersContext as a 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 views 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:

    namespace MvcDashboardDataSources {
        using System.Data.Entity;
    
        public partial class OrderContext : DbContext {
            public OrderContext()
                : base("name=OrderContext") {
            }
    
            public virtual DbSet<OrderDetails> OrderDetails { get; set; }
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder) {
                modelBuilder.Entity<OrderDetails>()
                    .Property(e => e.UnitPrice)
                    .HasPrecision(10, 4);
            }
        }
    }
    
  6. In the dashboard configuration file (for example, DashboardConfig.cs / DashboardConfig.vb), 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.DashboardWeb.Mvc;
    using DevExpress.DataAccess.EntityFramework;
    
    public static DataSourceInMemoryStorage CreateDataSourceStorage()
        DataSourceInMemoryStorage dataSourceStorage = new DataSourceInMemoryStorage();
    
        DashboardEFDataSource efDataSource = new DashboardEFDataSource("EF Data Source");
        efDataSource.ConnectionParameters = new EFConnectionParameters(typeof(OrdersContext));
        dataSourceStorage.RegisterDataSource("efDataSource", efDataSource.SaveToXml());
    
        return dataSourceStorage;
    }
    
  7. Call the DashboardConfigurator.SetDataSourceStorage method to configure the data source storage. Use the created CreateDataSourceStorage method as the SetDataSourceStorage parameter.

    using System;
    using DevExpress.DashboardWeb;
    using DevExpress.DataAccess.Json;
    
    public static void RegisterService(RouteCollection routes) {
        routes.MapDashboardRoute("dashboardControl", "DefaultDashboard");
    
        // ...
    
        DashboardConfigurator.Default.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 the ASP.NET MVC Dashboard Extension