Skip to content

Commit 2664a51

Browse files
committed
Import project
1 parent 9b26321 commit 2664a51

12 files changed

+447
-0
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
* text=auto
2+
*.cs diff=csharp
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<connectionStrings>
4+
<add name="TestConnection" connectionString="Data Source=Test.db" />
5+
</connectionStrings>
6+
</configuration>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net5.0</TargetFramework>
5+
<RootNamespace>EntityFrameworkCore.ConfigurationManager</RootNamespace>
6+
<AssemblyName>EntityFrameworkCore.ConfigurationManager.Tests</AssemblyName>
7+
<IsPackable>false</IsPackable>
8+
<LangVersion>8.0</LangVersion>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="System.Configuration.ConfigurationManager" Version="5.0.0-rc.2.20475.5" />
13+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="5.0.0-rc.2.20475.6" />
14+
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="5.0.0-rc.2.20475.6" />
15+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
16+
<PackageReference Include="xunit" Version="2.4.1" />
17+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
18+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
19+
<PrivateAssets>all</PrivateAssets>
20+
</PackageReference>
21+
</ItemGroup>
22+
23+
<ItemGroup>
24+
<ProjectReference Include="..\EFCore.ConfigurationManager\EFCore.ConfigurationManager.csproj" />
25+
</ItemGroup>
26+
27+
<ItemGroup>
28+
<!-- Work around xunit/xunit#1689 -->
29+
<None Update="App.config">
30+
<Link>testhost.dll.config</Link>
31+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
32+
</None>
33+
</ItemGroup>
34+
35+
</Project>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using Xunit;
3+
4+
namespace EntityFrameworkCore.ConfigurationManager
5+
{
6+
public class EndToEndTest
7+
{
8+
[Fact]
9+
public void Runtime_works()
10+
{
11+
using var db = new TestContext();
12+
13+
var dbConnection = db.Database.GetDbConnection();
14+
15+
Assert.Equal("Test.db", dbConnection.DataSource);
16+
}
17+
18+
class TestContext : DbContext
19+
{
20+
protected override void OnConfiguring(DbContextOptionsBuilder options)
21+
=> options
22+
.UseConfigurationManager()
23+
.UseSqlite("Name=TestConnection");
24+
}
25+
}
26+
}

EFCore.ConfigurationManager.sln

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.30626.31
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EFCore.ConfigurationManager", "EFCore.ConfigurationManager\EFCore.ConfigurationManager.csproj", "{FF9D1A68-4B90-4174-9D46-BAC7C5222269}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EFCore.ConfigurationManager.Tests", "EFCore.ConfigurationManager.Tests\EFCore.ConfigurationManager.Tests.csproj", "{CC07BE25-6F57-45AE-ADA8-B602B8EC580F}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{FF9D1A68-4B90-4174-9D46-BAC7C5222269}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{FF9D1A68-4B90-4174-9D46-BAC7C5222269}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{FF9D1A68-4B90-4174-9D46-BAC7C5222269}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{FF9D1A68-4B90-4174-9D46-BAC7C5222269}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{CC07BE25-6F57-45AE-ADA8-B602B8EC580F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{CC07BE25-6F57-45AE-ADA8-B602B8EC580F}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{CC07BE25-6F57-45AE-ADA8-B602B8EC580F}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{CC07BE25-6F57-45AE-ADA8-B602B8EC580F}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {1827D502-3EA1-4FA7-8051-15ECB84375B6}
30+
EndGlobalSection
31+
EndGlobal
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using Microsoft.EntityFrameworkCore.Diagnostics;
3+
using Microsoft.EntityFrameworkCore.Storage.Internal;
4+
5+
namespace EntityFrameworkCore.ConfigurationManager
6+
{
7+
class ConfigrationManagerConnectionStringResolver : INamedConnectionStringResolver
8+
{
9+
public string ResolveConnectionString(string connectionString)
10+
{
11+
var parts = connectionString.Split(new[] { '=' }, 2);
12+
if (parts.Length != 2
13+
|| !parts[0].Trim().Equals("Name", StringComparison.OrdinalIgnoreCase))
14+
{
15+
return connectionString;
16+
}
17+
18+
var connectionName = parts[1].Trim();
19+
20+
var element = System.Configuration.ConfigurationManager.ConnectionStrings[connectionName];
21+
if (element is null)
22+
{
23+
throw new InvalidOperationException(RelationalStrings.NamedConnectionStringNotFound(connectionName));
24+
}
25+
26+
return element.ConnectionString;
27+
}
28+
}
29+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using Microsoft.EntityFrameworkCore.Infrastructure;
3+
4+
namespace EntityFrameworkCore.ConfigurationManager
5+
{
6+
public static class ConfigurationManagerDbContextOptionsBuilder
7+
{
8+
public static DbContextOptionsBuilder UseConfigurationManager(this DbContextOptionsBuilder optionsBuilder)
9+
{
10+
var extension = optionsBuilder.Options.FindExtension<ConfigurationManagerDbContextOptionsExtension>()
11+
?? new ConfigurationManagerDbContextOptionsExtension();
12+
13+
((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(extension);
14+
15+
return optionsBuilder;
16+
}
17+
18+
public static DbContextOptionsBuilder<TContext> UseConfigurationManager<TContext>(this DbContextOptionsBuilder<TContext> optionsBuilder)
19+
where TContext : DbContext
20+
=> (DbContextOptionsBuilder<TContext>)UseConfigurationManager((DbContextOptionsBuilder)optionsBuilder);
21+
}
22+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using EntityFrameworkCore.ConfigurationManager.Properties;
4+
using Microsoft.EntityFrameworkCore.Infrastructure;
5+
using Microsoft.EntityFrameworkCore.Storage.Internal;
6+
using Microsoft.Extensions.DependencyInjection;
7+
8+
namespace EntityFrameworkCore.ConfigurationManager
9+
{
10+
class ConfigurationManagerDbContextOptionsExtension : IDbContextOptionsExtension
11+
{
12+
DbContextOptionsExtensionInfo _info;
13+
public DbContextOptionsExtensionInfo Info
14+
=> _info ??= new ExtensionInfo(this);
15+
16+
public void ApplyServices(IServiceCollection services)
17+
=> services.AddEntityFrameworkConfigurationManager();
18+
19+
public void Validate(IDbContextOptions options)
20+
{
21+
var internalServiceProvider = options.FindExtension<CoreOptionsExtension>()?.InternalServiceProvider;
22+
if (internalServiceProvider != null)
23+
{
24+
using var scope = internalServiceProvider.CreateScope();
25+
var resolver = scope.ServiceProvider.GetService<INamedConnectionStringResolver>();
26+
if (!(resolver is ConfigrationManagerConnectionStringResolver))
27+
{
28+
throw new InvalidOperationException(Resources.ServicesMissing);
29+
}
30+
}
31+
}
32+
33+
sealed class ExtensionInfo : DbContextOptionsExtensionInfo
34+
{
35+
public ExtensionInfo(IDbContextOptionsExtension extension)
36+
: base(extension)
37+
{
38+
}
39+
40+
public override bool IsDatabaseProvider
41+
=> false;
42+
43+
public override string LogFragment
44+
=> "using ConfigurationManager ";
45+
46+
public override long GetServiceProviderHashCode()
47+
=> 0;
48+
49+
public override void PopulateDebugInfo(IDictionary<string, string> debugInfo)
50+
=> debugInfo["ConfigurationManager"] = "1";
51+
}
52+
}
53+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Microsoft.EntityFrameworkCore.Infrastructure;
2+
using Microsoft.EntityFrameworkCore.Storage.Internal;
3+
using Microsoft.Extensions.DependencyInjection;
4+
5+
namespace EntityFrameworkCore.ConfigurationManager
6+
{
7+
public static class ConfigurationServiceCollectionExtensions
8+
{
9+
public static IServiceCollection AddEntityFrameworkConfigurationManager(this IServiceCollection serviceCollection)
10+
{
11+
new EntityFrameworkRelationalServicesBuilder(serviceCollection)
12+
.TryAddProviderSpecificServices(
13+
x => x.TryAddScoped<INamedConnectionStringResolver, ConfigrationManagerConnectionStringResolver>());
14+
15+
return serviceCollection;
16+
}
17+
}
18+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<RootNamespace>EntityFrameworkCore.ConfigurationManager</RootNamespace>
6+
<AssemblyName>EntityFrameworkCore.ConfigurationManager</AssemblyName>
7+
<LangVersion>8.0</LangVersion>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="3.1.9" />
12+
<PackageReference Include="System.Configuration.ConfigurationManager" Version="4.7.0" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<Compile Update="Properties\Resources.Designer.cs">
17+
<DesignTime>True</DesignTime>
18+
<AutoGen>True</AutoGen>
19+
<DependentUpon>Resources.resx</DependentUpon>
20+
</Compile>
21+
</ItemGroup>
22+
23+
<ItemGroup>
24+
<EmbeddedResource Update="Properties\Resources.resx">
25+
<Generator>ResXFileCodeGenerator</Generator>
26+
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
27+
</EmbeddedResource>
28+
</ItemGroup>
29+
30+
</Project>

0 commit comments

Comments
 (0)