/
CustomEFContextProviderFactory.cs
31 lines (30 loc) · 1.22 KB
/
CustomEFContextProviderFactory.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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();
}
}
}