Skip to main content
All docs
V24.1

IEFContextProvider Interface

When implemented, returns a context object to obtain from the dependency injection container.

Namespace: DevExpress.Data.Entity

Assembly: DevExpress.Data.v24.1.dll

NuGet Package: DevExpress.Data

Declaration

public interface IEFContextProvider

The following members return IEFContextProvider objects:

Remarks

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

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.

Refer to the examples below for more information about specifics related to the BI Dashboard and Reporting components.

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;
});

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

View Example

Implement the IEFContextProvider and IEFContextProviderFactory interfaces (the CustomEFContextProvider and CustomEFContextProviderFactory classes in this example) to create a service that allows you to get the EF Core Context.

using DevExpress.Data.Entity;
using DevExpress.DataAccess.Web;
using System;
using Microsoft.Extensions.DependencyInjection;
namespace WebEFCoreApp.Services {
    public class CustomEFContextProviderFactory : IEFContextProviderFactory {
        private readonly IServiceProvider serviceProvider;
        public CustomEFContextProviderFactory(IServiceProvider serviceProvider) {
            this.serviceProvider = serviceProvider;
        }
        public IEFContextProvider Create() {
            return new CustomEFContextProvider(serviceProvider.CreateScope());
        }
    }
    public class CustomEFContextProvider : IEFContextProvider, IDisposable {
        private readonly IServiceScope scope;
        public CustomEFContextProvider(IServiceScope scope) {
            this.scope = scope;
        }

        public object GetContext(string connectionName, Type contextType) {
            // Returns the context for the specified `EFDataSource.ConnectionName`. 
            if (connectionName == "efCoreConnection")
                return scope.ServiceProvider.GetRequiredService(contextType);
            return null;
        }
        public void Dispose() {
            scope.Dispose();
        }
    }
}

Register the context in the dependency injection container. Call the AddDbContext method in the ConfigureServices method of the Startup class to specify the required connection string. Use the ConfigureServices method of the Startup class to register the factory implementation.

namespace DXWebApplication1 {
    public class Startup {
        public Startup(IConfiguration configuration, IWebHostEnvironment hostingEnvironment) {
            Configuration = configuration;
        }
        public void ConfigureServices(IServiceCollection services) {
            // ...
            services.ConfigureReportingServices(configurator => {
                configurator.ConfigureWebDocumentViewer(viewerConfigurator => {
                    // ...
                    viewerConfigurator.RegisterEFContextProviderFactory<CustomEFContextProviderFactory>();
                });
                configurator.UseAsyncEngine();
            });
            services.AddDbContext<OrdersContext>(options => options.UseSqlite("Data Source=file:Data/nwind.db"), ServiceLifetime.Transient);
        }
    }
}
See Also