Skip to content

Commit

Permalink
Add Identity.EntityFrameworkCore
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisPulman committed Aug 30, 2023
1 parent 00eea90 commit deed65b
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 4 deletions.
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>
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)));
}
}
14 changes: 10 additions & 4 deletions src/CP.Extensions.Hosting.sln
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CP.Extensions.Hosting.WinFo
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CP.Extensions.Hosting.WinUI", "CP.Extensions.Hosting.WinUI\CP.Extensions.Hosting.WinUI.csproj", "{641302C9-7B83-42E3-8619-4FD1136B03FA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CP.Extensions.Hosting.ReactiveUI.Wpf", "CP.Extensions.Hosting.ReactiveUI.Wpf\CP.Extensions.Hosting.ReactiveUI.Wpf.csproj", "{FD76D9CB-9323-4555-B471-51CC517EBCBF}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CP.Extensions.Hosting.ReactiveUI.Wpf", "CP.Extensions.Hosting.ReactiveUI.Wpf\CP.Extensions.Hosting.ReactiveUI.Wpf.csproj", "{FD76D9CB-9323-4555-B471-51CC517EBCBF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CP.Extensions.Hosting.ReactiveUI.WinForms", "CP.Extensions.Hosting.ReactiveUI.WinForms\CP.Extensions.Hosting.ReactiveUI.WinForms.csproj", "{4481115F-6453-4E5A-B9A1-6B252438B391}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CP.Extensions.Hosting.ReactiveUI.WinForms", "CP.Extensions.Hosting.ReactiveUI.WinForms\CP.Extensions.Hosting.ReactiveUI.WinForms.csproj", "{4481115F-6453-4E5A-B9A1-6B252438B391}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CP.Extensions.Hosting.ReactiveUI.WinUI", "CP.Extensions.Hosting.ReactiveUI.WinUI\CP.Extensions.Hosting.ReactiveUI.WinUI.csproj", "{684344D8-09C7-4256-9DD7-5E3E14E68034}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CP.Extensions.Hosting.ReactiveUI.WinUI", "CP.Extensions.Hosting.ReactiveUI.WinUI\CP.Extensions.Hosting.ReactiveUI.WinUI.csproj", "{684344D8-09C7-4256-9DD7-5E3E14E68034}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CP.Extensions.Hosting.PluginService", "CP.Extensions.Hosting.PluginService\CP.Extensions.Hosting.PluginService.csproj", "{C4CC7AF8-E34E-4C5E-8E74-01946A7F97E4}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CP.Extensions.Hosting.PluginService", "CP.Extensions.Hosting.PluginService\CP.Extensions.Hosting.PluginService.csproj", "{C4CC7AF8-E34E-4C5E-8E74-01946A7F97E4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CP.Extensions.Hosting.Identity.EntityFrameworkCore", "CP.Extensions.Hosting.Identity.EntityFrameworkCore\CP.Extensions.Hosting.Identity.EntityFrameworkCore.csproj", "{36D4DB90-256B-46E2-9BB3-EB0FC2C40175}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -88,6 +90,10 @@ Global
{C4CC7AF8-E34E-4C5E-8E74-01946A7F97E4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C4CC7AF8-E34E-4C5E-8E74-01946A7F97E4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C4CC7AF8-E34E-4C5E-8E74-01946A7F97E4}.Release|Any CPU.Build.0 = Release|Any CPU
{36D4DB90-256B-46E2-9BB3-EB0FC2C40175}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{36D4DB90-256B-46E2-9BB3-EB0FC2C40175}.Debug|Any CPU.Build.0 = Debug|Any CPU
{36D4DB90-256B-46E2-9BB3-EB0FC2C40175}.Release|Any CPU.ActiveCfg = Release|Any CPU
{36D4DB90-256B-46E2-9BB3-EB0FC2C40175}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit deed65b

Please sign in to comment.