-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
00eea90
commit deed65b
Showing
3 changed files
with
110 additions
and
4 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
...ng.Identity.EntityFrameworkCore/CP.Extensions.Hosting.Identity.EntityFrameworkCore.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net6.0;net7.0</TargetFrameworks> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> | ||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup Condition=" $(TargetFramework.StartsWith('net6')) "> | ||
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="6.0.21" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="6.0.21" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.21"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup Condition=" $(TargetFramework.StartsWith('net7')) "> | ||
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="7.0.10" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="7.0.10" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.10"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
</Project> |
69 changes: 69 additions & 0 deletions
69
...tensions.Hosting.Identity.EntityFrameworkCore/HostBuilderEntityFrameworkCoreExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright (c) Chris Pulman. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
|
||
namespace CP.Extensions.Hosting.Identity.EntityFrameworkCore; | ||
|
||
/// <summary> | ||
/// Host Builder Entity Framework Core Extensions. | ||
/// </summary> | ||
public static class HostBuilderEntityFrameworkCoreExtensions | ||
{ | ||
/// <summary> | ||
/// Uses the entity framework core. | ||
/// </summary> | ||
/// <typeparam name="TContext">The type of the context.</typeparam> | ||
/// <param name="hostBuilder">The host builder.</param> | ||
/// <param name="connectionStringName">The connection string Name.</param> | ||
/// <param name="configureDatabase">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.</param> | ||
/// <param name="validateScopes"><c>true</c> to perform check verifying that scoped services never gets resolved from root provider; otherwise <c>false</c>. Defaults to <c>false</c>.</param> | ||
/// <returns> | ||
/// IHostBuilder. | ||
/// </returns> | ||
/// <exception cref="System.ArgumentNullException">hostBuilder.</exception> | ||
public static IHostBuilder UseEntityFrameworkCore<TContext>(this IHostBuilder hostBuilder, string? connectionStringName, Action<WebHostBuilderContext, IServiceCollection, DbContextOptionsBuilder, string> configureDatabase, bool validateScopes = false) | ||
where TContext : DbContext | ||
{ | ||
if (hostBuilder is null) | ||
{ | ||
throw new ArgumentNullException(nameof(hostBuilder)); | ||
} | ||
|
||
return hostBuilder.UseWebHostServices( | ||
(context, services) => | ||
{ | ||
var constring = context.Configuration.GetConnectionString(connectionStringName!) ?? throw new InvalidOperationException($"Connection string '{connectionStringName}' not found."); | ||
services.AddDbContext<TContext>(options => configureDatabase(context, services, options, constring)); | ||
}, | ||
validateScopes); | ||
} | ||
|
||
/// <summary> | ||
/// Uses the web host services. | ||
/// </summary> | ||
/// <param name="hostBuilder">The host builder.</param> | ||
/// <param name="configureServices">Adds a delegate for configuring additional services for the host or web application.</param> | ||
/// <param name="validateScopes"><c>true</c> to perform check verifying that scoped services never gets resolved from root provider; otherwise <c>false</c>. Defaults to <c>false</c>.</param> | ||
/// <returns>IHostBuilder.</returns> | ||
/// <exception cref="System.ArgumentNullException">hostBuilder.</exception> | ||
public static IHostBuilder UseWebHostServices(this IHostBuilder hostBuilder, Action<WebHostBuilderContext, IServiceCollection> configureServices, bool validateScopes = false) | ||
{ | ||
if (hostBuilder is null) | ||
{ | ||
throw new ArgumentNullException(nameof(hostBuilder)); | ||
} | ||
|
||
return hostBuilder.ConfigureWebHostDefaults(webBuilder => | ||
webBuilder.UseDefaultServiceProvider(options => options.ValidateScopes = validateScopes) | ||
.Configure(app => app.Run(async (_) => await Task.CompletedTask)) // Dummy app.Run to prevent 'No application service provider was found' error. | ||
.ConfigureServices((context, services) => configureServices(context, services))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters