Skip to main content

Entity Framework Data Source in ASP.NET Core

  • 3 minutes to read

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

Prerequisites

The ASP.NET Core Dashboard control supports the following Entity Framework versions:

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

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

Create a Data Context

Add a database to your application. In this example, it is the nwind.db database. You can find this and other sample databases in the following directory:

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

Then, add a new class and create a data context based on the database. Define a connection string to the SQLite database by overriding the OnConfiguring method in the constructor. In this example, the created class is based on the Orders table of the nwind.db database.

using Microsoft.EntityFrameworkCore;
using System;
using System.ComponentModel.DataAnnotations;
using System.Data.SQLite;

namespace WebDashboardDataSources
{
    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; }
    }
}

Configure an Entity Framework Data Source

Create a Data Source

To create an Entity Framework Data Source, follow the steps below:

using DevExpress.DashboardCommon;
using DevExpress.DashboardWeb;
using DevExpress.DataAccess.EntityFramework;

DashboardEFDataSource efDataSource = new DashboardEFDataSource("EF Data Source");            
efDataSource.ConnectionParameters = new EFConnectionParameters(typeof(OrdersContext));

Register the Data Source in the Storage

Call the DataSourceInMemoryStorage.RegisterDataSource method to register the data source in the data source storage. Call the DashboardConfigurator.SetDataSourceStorage method to specify the data source storage for the Web Dashboard.

// Create a data source storage.
DataSourceInMemoryStorage dataSourceStorage = new DataSourceInMemoryStorage();

// Register the Entity Framework data source.
dataSourceStorage.RegisterDataSource("efDataSource", efDataSource.SaveToXml());

// Register the storage for the Web Dashboard.
configurator.SetDataSourceStorage(dataSourceStorage);

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

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

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

Example: How to Register Data Sources for ASP.NET Core Dashboard Control

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

View Example

Example: Resolve the Entity Framework Core Context from the DI Container

The following example shows how to obtain the Entity Framework Core context from the ASP.NET Core dependency injection container:

View Example