/
EntityFrameworkServiceCollectionExtensions.cs
173 lines (162 loc) · 9.74 KB
/
EntityFrameworkServiceCollectionExtensions.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
// ReSharper disable once CheckNamespace
namespace Microsoft.Extensions.DependencyInjection
{
/// <summary>
/// Extension methods for setting up Entity Framework related services in an <see cref="IServiceCollection" />.
/// </summary>
public static class EntityFrameworkServiceCollectionExtensions
{
/// <summary>
/// Registers the given context as a service in the <see cref="IServiceCollection" />.
/// You use this method when using dependency injection in your application, such as with ASP.NET.
/// For more information on setting up dependency injection, see http://go.microsoft.com/fwlink/?LinkId=526890.
/// </summary>
/// <example>
/// <code>
/// public void ConfigureServices(IServiceCollection services)
/// {
/// var connectionString = "connection string to database";
///
/// services.AddDbContext<MyContext>(options => options.UseSqlServer(connectionString));
/// }
/// </code>
/// </example>
/// <typeparam name="TContext"> The type of context to be registered. </typeparam>
/// <param name="serviceCollection"> The <see cref="IServiceCollection" /> to add services to. </param>
/// <param name="optionsAction">
/// <para>
/// An optional action to configure the <see cref="DbContextOptions" /> for the context. This provides an
/// alternative to performing configuration of the context by overriding the
/// <see cref="DbContext.OnConfiguring" /> method in your derived context.
/// </para>
/// <para>
/// If an action is supplied here, the <see cref="DbContext.OnConfiguring" /> method will still be run if it has
/// been overridden on the derived context. <see cref="DbContext.OnConfiguring" /> configuration will be applied
/// in addition to configuration performed here.
/// </para>
/// <para>
/// In order for the options to be passed into your context, you need to expose a constructor on your context that takes
/// <see cref="DbContextOptions{TContext}" /> and passes it to the base constructor of <see cref="DbContext"/>.
/// </para>
/// </param>
/// <param name="contextLifetime"> The lifetime with which to register the DbContext service in the container. </param>
/// <returns>
/// A builder that allows further Entity Framework specific setup of the <see cref="IServiceCollection" />.
/// </returns>
public static IServiceCollection AddDbContext<TContext>(
[NotNull] this IServiceCollection serviceCollection,
[CanBeNull] Action<DbContextOptionsBuilder> optionsAction = null,
ServiceLifetime contextLifetime = ServiceLifetime.Scoped)
where TContext : DbContext
=> AddDbContext<TContext>(serviceCollection, (p, b) => optionsAction?.Invoke(b), contextLifetime);
/// <summary>
/// Registers the given context as a service in the <see cref="IServiceCollection" />.
/// You use this method when using dependency injection in your application, such as with ASP.NET.
/// For more information on setting up dependency injection, see http://go.microsoft.com/fwlink/?LinkId=526890.
/// </summary>
/// <example>
/// <code>
/// public void ConfigureServices(IServiceCollection services)
/// {
/// var connectionString = "connection string to database";
///
/// services.AddDbContext<MyContext>(ServiceLifetime.Scoped);
/// }
/// </code>
/// </example>
/// <typeparam name="TContext"> The type of context to be registered. </typeparam>
/// <param name="serviceCollection"> The <see cref="IServiceCollection" /> to add services to. </param>
/// <param name="contextLifetime"> The lifetime with which to register the DbContext service in the container. </param>
/// <returns>
/// A builder that allows further Entity Framework specific setup of the <see cref="IServiceCollection" />.
/// </returns>
public static IServiceCollection AddDbContext<TContext>(
[NotNull] this IServiceCollection serviceCollection,
ServiceLifetime contextLifetime)
where TContext : DbContext
=> AddDbContext<TContext>(serviceCollection, (Action<IServiceProvider, DbContextOptionsBuilder>)null, contextLifetime);
/// <summary>
/// <para>
/// Registers the given context as a service in the <see cref="IServiceCollection" />.
/// You use this method when using dependency injection in your application, such as with ASP.NET.
/// For more information on setting up dependency injection, see http://go.microsoft.com/fwlink/?LinkId=526890.
/// </para>
/// <para>
/// This overload has an <paramref name="optionsAction"/> that provides the applications <see cref="IServiceProvider"/>.
/// This is useful if you want to setup Entity Framework to resolve its internal services from the primary application service provider.
/// By default, we recommend using the other overload, which allows Entity Framework to create and maintain its own <see cref="IServiceProvider"/>
/// for internal Entity Framework services.
/// </para>
/// </summary>
/// <example>
/// <code>
/// public void ConfigureServices(IServiceCollection services)
/// {
/// var connectionString = "connection string to database";
///
/// services.AddDbContext<MyContext>((serviceProvider, options) =>
/// options.UseSqlServer(connectionString)
/// .UseInternalServiceProvider(serviceProvider));
/// }
/// </code>
/// </example>
/// <typeparam name="TContext"> The type of context to be registered. </typeparam>
/// <param name="serviceCollection"> The <see cref="IServiceCollection" /> to add services to. </param>
/// <param name="optionsAction">
/// <para>
/// An optional action to configure the <see cref="DbContextOptions" /> for the context. This provides an
/// alternative to performing configuration of the context by overriding the
/// <see cref="DbContext.OnConfiguring" /> method in your derived context.
/// </para>
/// <para>
/// If an action is supplied here, the <see cref="DbContext.OnConfiguring" /> method will still be run if it has
/// been overridden on the derived context. <see cref="DbContext.OnConfiguring" /> configuration will be applied
/// in addition to configuration performed here.
/// </para>
/// <para>
/// In order for the options to be passed into your context, you need to expose a constructor on your context that takes
/// <see cref="DbContextOptions{TContext}" /> and passes it to the base constructor of <see cref="DbContext"/>.
/// </para>
/// </param>
/// <param name="contextLifetime"> The lifetime with which to register the DbContext service in the container. </param>
/// <returns>
/// A builder that allows further Entity Framework specific setup of the <see cref="IServiceCollection" />.
/// </returns>
public static IServiceCollection AddDbContext<TContext>(
[NotNull] this IServiceCollection serviceCollection,
[CanBeNull] Action<IServiceProvider, DbContextOptionsBuilder> optionsAction,
ServiceLifetime contextLifetime = ServiceLifetime.Scoped)
where TContext : DbContext
{
serviceCollection
.AddMemoryCache()
.AddLogging();
serviceCollection.TryAddSingleton(p => DbContextOptionsFactory<TContext>(p, optionsAction));
serviceCollection.AddSingleton<DbContextOptions>(p => p.GetRequiredService<DbContextOptions<TContext>>());
serviceCollection.TryAdd(new ServiceDescriptor(typeof(TContext), typeof(TContext), contextLifetime));
return serviceCollection;
}
private static DbContextOptions<TContext> DbContextOptionsFactory<TContext>(
[NotNull] IServiceProvider applicationServiceProvider,
[CanBeNull] Action<IServiceProvider, DbContextOptionsBuilder> optionsAction)
where TContext : DbContext
{
var builder = new DbContextOptionsBuilder<TContext>(
new DbContextOptions<TContext>(new Dictionary<Type, IDbContextOptionsExtension>()))
.UseMemoryCache(applicationServiceProvider.GetService<IMemoryCache>())
.UseLoggerFactory(applicationServiceProvider.GetService<ILoggerFactory>());
optionsAction?.Invoke(applicationServiceProvider, builder);
return builder.Options;
}
}
}