Skip to content

Commit 29002d4

Browse files
committed
Moved conn string of the sample app to appsettings.
1 parent 1fab5d8 commit 29002d4

4 files changed

Lines changed: 29 additions & 1 deletion

File tree

Directory.Packages.props

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
<PrivateAssets>all</PrivateAssets>
2020
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2121
</PackageVersion>
22+
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="9.0.7" />
23+
<PackageVersion Include="Microsoft.Extensions.Configuration.Json" Version="9.0.7" />
24+
<PackageVersion Include="Microsoft.Extensions.Configuration.UserSecrets" Version="9.0.7" />
2225
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.7" />
2326
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="9.0.7" />
2427
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />

samples/EntityFrameworkCore.Samples/EntityFrameworkCore.Samples.csproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<OutputType>EXE</OutputType>
4+
<UserSecretsId>0b057f42-0677-472f-903c-eb2dacd094f3</UserSecretsId>
45
</PropertyGroup>
56
<ItemGroup>
67
<ProjectReference Include="..\..\src\Thinktecture.Runtime.Extensions.EntityFrameworkCore8\Thinktecture.Runtime.Extensions.EntityFrameworkCore8.csproj" />
@@ -9,7 +10,15 @@
910
</ItemGroup>
1011
<ItemGroup>
1112
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" />
13+
<PackageReference Include="Microsoft.Extensions.Configuration" />
14+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" />
15+
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" />
1216
<PackageReference Include="Serilog.Extensions.Logging" />
1317
<PackageReference Include="Serilog.Sinks.Console" />
1418
</ItemGroup>
19+
<ItemGroup>
20+
<None Update="appsettings.json">
21+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
22+
</None>
23+
</ItemGroup>
1524
</Project>

samples/EntityFrameworkCore.Samples/Program.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Linq;
33
using System.Threading.Tasks;
44
using Microsoft.EntityFrameworkCore;
5+
using Microsoft.Extensions.Configuration;
56
using Microsoft.Extensions.DependencyInjection;
67
using Microsoft.Extensions.Logging;
78
using Serilog;
@@ -144,6 +145,8 @@ private static async Task DoMessagesDemosAsync(IServiceProvider serviceProvider,
144145

145146
private static IServiceProvider CreateServiceProvider(LoggingLevelSwitch loggingLevelSwitch)
146147
{
148+
var configuration = GetConfiguration();
149+
147150
return new ServiceCollection()
148151
.AddLogging(builder =>
149152
{
@@ -157,7 +160,7 @@ private static IServiceProvider CreateServiceProvider(LoggingLevelSwitch logging
157160

158161
builder.AddSerilog(serilogLogger);
159162
})
160-
.AddDbContext<ProductsDbContext>(builder => builder.UseSqlServer("Server=localhost;Database=TT-Runtime-Extensions-Demo;Integrated Security=true;TrustServerCertificate=true")
163+
.AddDbContext<ProductsDbContext>(builder => builder.UseSqlServer(configuration.GetConnectionString("default"))
161164
.EnableSensitiveDataLogging()
162165
.UseThinktectureValueConverters(configureEnumsAndKeyedValueObjects: property =>
163166
{
@@ -179,6 +182,14 @@ private static IServiceProvider CreateServiceProvider(LoggingLevelSwitch logging
179182
.BuildServiceProvider();
180183
}
181184

185+
private static IConfiguration GetConfiguration()
186+
{
187+
return new ConfigurationBuilder()
188+
.AddJsonFile("appsettings.json")
189+
.AddUserSecrets<Program>()
190+
.Build();
191+
}
192+
182193
private static int RoundUp(int value)
183194
{
184195
return value + (10 - value % 10);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"ConnectionStrings": {
3+
"DefaultConnection": "Server=localhost;Database=TT-Runtime-Extensions-Demo;Integrated Security=true;TrustServerCertificate=true"
4+
}
5+
}

0 commit comments

Comments
 (0)