IEFContextProviderFactory Interface
When implemented, supplies a service that allows you to obtain the EF Core context from the dependency injection container.
Namespace: DevExpress.DataAccess.Web
Assembly: DevExpress.DataAccess.v24.1.dll
NuGet Packages: DevExpress.DataAccess, DevExpress.Win.PivotGrid, DevExpress.Win.TreeMap
Declaration
Remarks
You can resolve the appropriate Entity Framework Core context from the ASP.NET Core service container for dashboards and reports bound to EFDataSource.
For this, implement the 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: Reporting for ASP.NET Core - Resolve the Entity Framework Core Context from the DI Container
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);
}
}
}