Skip to main content
All docs
V25.1
  • WebDocumentViewerConfigurationBuilder.RegisterEFContextProviderFactory<T>() Method

    Registers a custom factory that allows you to obtain the EF Core context from the dependency injection container.

    Namespace: DevExpress.AspNetCore.Reporting

    Assembly: DevExpress.AspNetCore.Reporting.v25.1.dll

    NuGet Package: DevExpress.AspNetCore.Reporting

    Declaration

    public WebDocumentViewerConfigurationBuilder RegisterEFContextProviderFactory<T>()
        where T : class, IEFContextProviderFactory

    Type Parameters

    Name Description
    T

    A custom factory that implements the IEFContextProviderFactory interface.

    Returns

    Type Description
    WebDocumentViewerConfigurationBuilder

    A WebDocumentViewerConfigurationBuilder that can be used to further configure the Web Document Viewer services.

    Remarks

    You can resolve the appropriate Entity Framework Core context from the ASP.NET Core dependency injection container for 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.

    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 at the application startup to specify the connection string and the ConfigureReportingServices method to register the factory implementation.

    var builder = WebApplication.CreateBuilder(args);
    
    builder.Services.ConfigureReportingServices(configurator => {
        configurator.ConfigureWebDocumentViewer(viewerConfigurator => {
            viewerConfigurator.RegisterEFContextProviderFactory<CustomEFContextProviderFactory>();
        });
        configurator.UseAsyncEngine();
    });
    builder.Services.AddDbContext<OrdersContext>(options => options.UseSqlite("Data Source=file:Data/nwind.db"), ServiceLifetime.Transient);
    
    var app = builder.Build();
    
    See Also