-
Notifications
You must be signed in to change notification settings - Fork 2k
Document ConfigureDbContext method and AddDbContext precedence changes in EF Core 8.0 #5118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: live
Are you sure you want to change the base?
Changes from 7 commits
aed0fbb
3c2e6f2
fc3ce37
5d80627
c6277c3
e544d44
52ec51e
1072ea3
fc950c9
ce6de19
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
AndriySvyryd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.0" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="9.0.0" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
using System; | ||
using System.Linq; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace ConfigureDbContextSample; | ||
|
||
public class BlogContext : DbContext | ||
{ | ||
public BlogContext(DbContextOptions<BlogContext> options) : base(options) | ||
{ | ||
} | ||
|
||
public DbSet<Blog> Blogs => Set<Blog>(); | ||
} | ||
|
||
public class Blog | ||
{ | ||
public int Id { get; set; } | ||
public required string Title { get; set; } | ||
public string? Content { get; set; } | ||
Check warning on line 21 in samples/core/Miscellaneous/ConfiguringDbContext/ConfigureDbContextSample/Program.cs
|
||
} | ||
|
||
public class Program | ||
{ | ||
public static void Main(string[] args) | ||
Check warning on line 26 in samples/core/Miscellaneous/ConfiguringDbContext/ConfigureDbContextSample/Program.cs
|
||
AndriySvyryd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
{ | ||
BasicConfigureDbContextExample(); | ||
ProviderSpecificConfigurationExample(); | ||
ConfigurationCompositionExample(); | ||
} | ||
|
||
private static void BasicConfigureDbContextExample() | ||
{ | ||
Console.WriteLine("=== Basic ConfigureDbContext Example ==="); | ||
|
||
#region BasicConfigureDbContext | ||
var services = new ServiceCollection(); | ||
|
||
services.ConfigureDbContext<BlogContext>(options => | ||
Check failure on line 40 in samples/core/Miscellaneous/ConfiguringDbContext/ConfigureDbContextSample/Program.cs
|
||
options.EnableSensitiveDataLogging() | ||
.EnableDetailedErrors()); | ||
|
||
services.AddDbContext<BlogContext>(options => | ||
options.UseInMemoryDatabase("BasicExample")); | ||
Check failure on line 45 in samples/core/Miscellaneous/ConfiguringDbContext/ConfigureDbContextSample/Program.cs
|
||
#endregion | ||
AndriySvyryd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
var serviceProvider = services.BuildServiceProvider(); | ||
using var scope = serviceProvider.CreateScope(); | ||
var context = scope.ServiceProvider.GetRequiredService<BlogContext>(); | ||
|
||
Console.WriteLine($"Context configured with provider: {context.Database.ProviderName}"); | ||
Console.WriteLine(); | ||
} | ||
|
||
private static void ProviderSpecificConfigurationExample() | ||
{ | ||
Console.WriteLine("=== Provider-Specific Configuration Example ==="); | ||
|
||
#region ProviderSpecificConfiguration | ||
var services = new ServiceCollection(); | ||
|
||
services.ConfigureDbContext<BlogContext>(options => | ||
Check failure on line 63 in samples/core/Miscellaneous/ConfiguringDbContext/ConfigureDbContextSample/Program.cs
|
||
options.UseSqlServer(sqlOptions => | ||
sqlOptions.EnableRetryOnFailure())); | ||
|
||
services.AddDbContext<BlogContext>(options => | ||
options.UseSqlServer("connectionString")); | ||
#endregion | ||
AndriySvyryd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
var serviceProvider = services.BuildServiceProvider(); | ||
Console.WriteLine("Provider-specific configuration applied"); | ||
Console.WriteLine(); | ||
} | ||
|
||
private static void ConfigurationCompositionExample() | ||
{ | ||
Console.WriteLine("=== Configuration Composition Example ==="); | ||
|
||
#region ConfigurationComposition | ||
var services = new ServiceCollection(); | ||
|
||
services.ConfigureDbContext<BlogContext>(options => | ||
Check failure on line 83 in samples/core/Miscellaneous/ConfiguringDbContext/ConfigureDbContextSample/Program.cs
|
||
options.LogTo(Console.WriteLine)); | ||
|
||
services.AddDbContext<BlogContext>(options => | ||
options.UseInMemoryDatabase("CompositionExample")); | ||
Check failure on line 87 in samples/core/Miscellaneous/ConfiguringDbContext/ConfigureDbContextSample/Program.cs
|
||
|
||
services.ConfigureDbContext<BlogContext>(options => | ||
Check failure on line 89 in samples/core/Miscellaneous/ConfiguringDbContext/ConfigureDbContextSample/Program.cs
|
||
options.EnableSensitiveDataLogging()); | ||
#endregion | ||
AndriySvyryd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
var serviceProvider = services.BuildServiceProvider(); | ||
using var scope = serviceProvider.CreateScope(); | ||
var context = scope.ServiceProvider.GetRequiredService<BlogContext>(); | ||
|
||
Console.WriteLine($"Context configured with provider: {context.Database.ProviderName}"); | ||
Console.WriteLine(); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.