Skip to main content
All docs
V24.2

DashboardConfigurator.SetEFContextProvider(IEFContextProvider) Method

Configures the service that creates an EF Core context object within DashboardConfigurator.

Namespace: DevExpress.DashboardWeb

Assembly: DevExpress.Dashboard.v24.2.Web.dll

Declaration

public void SetEFContextProvider(
    IEFContextProvider efContextProvider
)

Parameters

Name Type Description
efContextProvider IEFContextProvider

A DevExpress.Data.Entity.IEFContextProvider object.

Remarks

You can resolve the appropriate Entity Framework Core context from the ASP.NET Core dependency injection container for dashboards bound to DashboardEFDataSource.

For this, implement a custom service that creates a EF Core context object for the specified data source. Register this service in IServiceCollection to obtain the EF Core context from the DI container.

Example: Dashboard for ASP.NET Core - Resolve the Entity Framework Core Context from the DI Container

View Example

Implement the IEFContextProvider interface (the CustomEFContextProvider class in this example) to create a service that allows you to get the EF Core Context.

Call the IEFContextProvider.GetContext(String, Type) method to return the context for the specified data source.

using DevExpress.Data.Entity;
using Microsoft.EntityFrameworkCore;
using System;

namespace WebEFCoreApp {
    public class CustomEFContextProvider : IEFContextProvider, IDisposable {
        IServiceScope scope;
        public CustomEFContextProvider(IServiceProvider provider) {
            this.scope = provider.CreateScope();
        }
        public object GetContext(string connectionName, Type contextType) {
            // Returns the context for the specified `EFDataSource.ConnectionName`. 
            if (connectionName == "EF Data Connection") {
                return scope.ServiceProvider.GetRequiredService(contextType);
            }
            return null;
        }
        public void Dispose() {
            scope.Dispose();
        }
    }

}

Call the AddDbContext method to register the context in the dependency injection container and specify the connection string in the WebApplicationBuilder class.

using Microsoft.EntityFrameworkCore;
using DevExpress.DashboardWeb;
using DevExpress.AspNetCore;
using System;

var builder = WebApplication.CreateBuilder(args);
// ...
builder.Services.AddDbContext<OrdersContext>(options => options.UseSqlite("Data Source=file:Data/nwind.db"), ServiceLifetime.Transient);
var app = builder.Build();
// ...

In the WebApplicationBuilder class, register the DashboardConfigurator service and configure it using the SetEFContextProvider(IEFContextProvider) method.

using Microsoft.EntityFrameworkCore;
using DevExpress.DashboardWeb;
using DevExpress.AspNetCore;
using System;
// ...
builder.Services.AddScoped<DashboardConfigurator>((IServiceProvider serviceProvider) => {

DashboardConfigurator configurator = new DashboardConfigurator();
configurator.SetEFContextProvider(new CustomEFContextProvider(serviceProvider));
// ...
return configurator;
});
See Also