-
-
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
b131bea
commit 4231076
Showing
5 changed files
with
242 additions
and
20 deletions.
There are no files selected for viewing
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
125 changes: 125 additions & 0 deletions
125
...s.Hosting.Identity.EntityFrameworkCore.Sqlite/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,125 @@ | ||
// 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 with Sqlite. | ||
/// </summary> | ||
/// <typeparam name="TContext">The type of the context.</typeparam> | ||
/// <typeparam name="TUser">The type of the user.</typeparam> | ||
/// <typeparam name="TRole">The type of the role.</typeparam> | ||
/// <param name="services">The services.</param> | ||
/// <param name="context">The context.</param> | ||
/// <param name="connectionStringName">Name of the connection string.</param> | ||
/// <returns> | ||
/// IServiceCollection. | ||
/// </returns> | ||
/// <exception cref="ArgumentNullException">services | ||
/// or | ||
/// context.</exception> | ||
public static IServiceCollection UseEntityFrameworkCoreSqlite<TContext, TUser, TRole>(this IServiceCollection services, WebHostBuilderContext context, string connectionStringName) | ||
where TContext : DbContext | ||
where TUser : class | ||
where TRole : class | ||
{ | ||
if (services == null) | ||
{ | ||
throw new ArgumentNullException(nameof(services)); | ||
} | ||
|
||
if (context == null) | ||
{ | ||
throw new ArgumentNullException(nameof(context)); | ||
} | ||
|
||
if (string.IsNullOrWhiteSpace(connectionStringName)) | ||
{ | ||
throw new ArgumentException("Value cannot be null or whitespace.", nameof(connectionStringName)); | ||
} | ||
|
||
var conString = context.Configuration.GetConnectionString(connectionStringName); | ||
services.AddDbContext<TContext>(options => | ||
options.UseSqlite(conString ?? | ||
throw new InvalidOperationException($"Connection string '{connectionStringName}' not found."))) | ||
.AddDefaultIdentity<TUser>() | ||
.AddRoles<TRole>() | ||
.AddEntityFrameworkStores<TContext>(); | ||
return services; | ||
} | ||
|
||
/// <summary> | ||
/// Uses the entity framework core with Sqlite. | ||
/// </summary> | ||
/// <typeparam name="TContext">The type of the context.</typeparam> | ||
/// <typeparam name="TUser">The type of the user.</typeparam> | ||
/// <param name="services">The services.</param> | ||
/// <param name="context">The context.</param> | ||
/// <param name="connectionStringName">Name of the connection string.</param> | ||
/// <returns> | ||
/// IServiceCollection. | ||
/// </returns> | ||
/// <exception cref="ArgumentNullException">services | ||
/// or | ||
/// context.</exception> | ||
public static IServiceCollection UseEntityFrameworkCoreSqlite<TContext, TUser>(this IServiceCollection services, WebHostBuilderContext context, string connectionStringName) | ||
where TContext : DbContext | ||
where TUser : class | ||
{ | ||
if (services == null) | ||
{ | ||
throw new ArgumentNullException(nameof(services)); | ||
} | ||
|
||
if (context == null) | ||
{ | ||
throw new ArgumentNullException(nameof(context)); | ||
} | ||
|
||
if (string.IsNullOrWhiteSpace(connectionStringName)) | ||
{ | ||
throw new ArgumentException("Value cannot be null or whitespace.", nameof(connectionStringName)); | ||
} | ||
|
||
var conString = context.Configuration.GetConnectionString(connectionStringName); | ||
services.AddDbContext<TContext>(options => | ||
options.UseSqlite(conString ?? | ||
throw new InvalidOperationException($"Connection string '{connectionStringName}' not found."))) | ||
.AddDefaultIdentity<TUser>() | ||
.AddEntityFrameworkStores<TContext>(); | ||
return services; | ||
} | ||
|
||
/// <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))); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...y.EntityFrameworkCore/CP.Extensions.Hosting.Identity.EntityFrameworkCore.SqlServer.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,33 @@ | ||
<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.SqlServer" 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.SqlServer" 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> |
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
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