The requested page is not available for the requested platform. You are viewing the content for Default platform.

Connect the ASP.NET Core Dashboard to an Entity Framework data source

  • 3 min to read

The example below shows how to add the DashboardEFDataSource to the data source storage and make it available to end users. This example uses the SQLite database.

Important

Before you begin, open the NuGet Package Manager and install the Microsoft.EntityFrameworkCore.Sqlite package. This is the SQLite database provider for the Entity Framework Core.

  1. In your application, create the Data folder and add the nwind.db database to it from the following folder:

    C:\Users\Public\Documents\DevExpress Demos 19.2\Components\Data

  2. Add a new OrdersContext.cs class and create a data context based on the Orders table of the nwind.db database. Define a connection string to the SQLite database by overriding the OnConfiguring method in the constructor.

    using Microsoft.EntityFrameworkCore;
    using System;
    using System.ComponentModel.DataAnnotations;
    
    namespace MvcCoreDashboard {
    
        public partial class OrdersContext : DbContext {
            public OrdersContext() : base() { }
            protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
                base.OnConfiguring(optionsBuilder);
                optionsBuilder.UseSqlite("Data Source=file:Data/nwind.db");
            }
            public virtual DbSet<Order> Orders { get; set; }
        }
        public class Order {
            [Key]
            public int OrderID { get; set; }
            public string CustomerID { get; set; }
            public long? EmployeeID { get; set; }
            public DateTime? OrderDate { get; set; }
            public DateTime? RequiredDate { get; set; }
            public DateTime? ShippedDate { get; set; }
            public long? ShipVia { get; set; }
            public decimal? Freight { get; set; }
            public string ShipName { get; set; }
            public string ShipAddress { get; set; }
            public string ShipCity { get; set; }
            public string ShipRegion { get; set; }
            public string ShipPostalCode { get; set; }
            public string ShipCountry { get; set; }
        }
    }
    
  3. In the Startup.cs file, create a public method that returns a configured in-memory dashboard data source storage (DataSourceInMemoryStorage) and define the Entity Framework Core data source.

    using DevExpress.DashboardCommon;
    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;
    }
    
  4. Call the DashboardConfigurator.SetDataSourceStorage method to configure the data source storage. Use the created CreateDataSourceStorage method as the SetDataSourceStorage parameter.

    using DevExpress.AspNetCore;
    using DevExpress.DashboardAspNetCore;
    using DevExpress.DashboardWeb;
    
    public void ConfigureServices(IServiceCollection services) {            
        services
            .AddMvc()
            .AddDefaultDashboardController(configurator => {
                // ...
                configurator.SetDataSourceStorage(CreateDataSourceStorage());
            });
    }
    
  5. As a result, the EF Core Data Source is displayed as a Web Dashboard's predefined data source.

    web-dashboard-ex-core-ef-core--data-source

    End users can now bind the dashboard items to data in the Web Dashboard's UI. See Binding Dashboard Items to Data in the Web Dashboard's UI for more information.

Example

The example displays how to provide a Web Dashboard with a set of predefined data sources available for end users.