-
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
Open
Copilot
wants to merge
10
commits into
live
Choose a base branch
from
copilot/fix-d48d3f71-d338-425e-a4bd-f70e7fa2179d
base: live
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
aed0fbb
Initial plan
Copilot 3c2e6f2
Add documentation for multiple AddDbContext calls behavior in EF Core…
Copilot fc3ce37
Final documentation updates for AddDbContext precedence behavior
Copilot 5d80627
Update documentation to properly document ConfigureDbContext method a…
Copilot c6277c3
Refine ConfigureDbContext documentation based on review feedback
Copilot e544d44
Apply final review feedback - update ms.date, change WARNING to NOTE,…
Copilot 52ec51e
Move endregion tags to exclude var serviceProvider line from code sni…
Copilot 1072ea3
Move ConfigureDbContext sample code to main project and remove separa…
Copilot fc950c9
Update all package references to version 9.0.0 and target framework t…
Copilot ce6de19
Rename ConfigureDbContextExamples class to ConfigureDbContextSample
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
17 changes: 17 additions & 0 deletions
17
...scellaneous/ConfiguringDbContext/ConfigureDbContextSample/ConfigureDbContextSample.csproj
This file contains hidden or 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,17 @@ | ||
<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.Extensions.DependencyInjection" Version="9.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
134 changes: 134 additions & 0 deletions
134
samples/core/Miscellaneous/ConfiguringDbContext/ConfigureDbContextSample/Program.cs
This file contains hidden or 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,134 @@ | ||
using System; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
|
||
namespace ConfigureDbContextSample; | ||
|
||
#region BlogContext | ||
AndriySvyryd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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; } | ||
} | ||
#endregion | ||
|
||
#region TestingExtensions | ||
public static class TestingExtensions | ||
{ | ||
public static IServiceCollection AddTestingConfiguration<TContext>( | ||
this IServiceCollection services) | ||
where TContext : DbContext | ||
{ | ||
services.ConfigureDbContext<TContext>(options => | ||
options.EnableSensitiveDataLogging() | ||
.EnableDetailedErrors() | ||
.LogTo(Console.WriteLine)); | ||
|
||
return services; | ||
} | ||
} | ||
#endregion | ||
|
||
public class Program | ||
{ | ||
public static void Main(string[] args) | ||
AndriySvyryd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
{ | ||
BasicConfigureDbContextExample(); | ||
ReusableComponentExample(); | ||
ConfigurationCompositionExample(); | ||
} | ||
|
||
private static void BasicConfigureDbContextExample() | ||
{ | ||
Console.WriteLine("=== Basic ConfigureDbContext Example ==="); | ||
|
||
#region BasicConfigureDbContext | ||
var services = new ServiceCollection(); | ||
|
||
// Configure non-provider-specific options | ||
services.ConfigureDbContext<BlogContext>(options => | ||
options.EnableSensitiveDataLogging() | ||
.EnableDetailedErrors()); | ||
|
||
// Add the context with provider configuration | ||
services.AddDbContext<BlogContext>(options => | ||
options.UseInMemoryDatabase("BasicExample")); | ||
|
||
var serviceProvider = services.BuildServiceProvider(); | ||
#endregion | ||
AndriySvyryd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
using var scope = serviceProvider.CreateScope(); | ||
var context = scope.ServiceProvider.GetRequiredService<BlogContext>(); | ||
|
||
Console.WriteLine($"Context configured with provider: {context.Database.ProviderName}"); | ||
Console.WriteLine("ConfigureDbContext was called before AddDbContext"); | ||
Console.WriteLine(); | ||
} | ||
|
||
private static void ReusableComponentExample() | ||
{ | ||
Console.WriteLine("=== Reusable Component Example ==="); | ||
|
||
#region ReusableComponent | ||
var services = new ServiceCollection(); | ||
|
||
// Use the testing extension method | ||
services.AddTestingConfiguration<BlogContext>(); | ||
|
||
// Add the context with provider | ||
services.AddDbContext<BlogContext>(options => | ||
options.UseInMemoryDatabase("TestDb")); | ||
|
||
var serviceProvider = services.BuildServiceProvider(); | ||
#endregion | ||
AndriySvyryd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
using var scope = serviceProvider.CreateScope(); | ||
var context = scope.ServiceProvider.GetRequiredService<BlogContext>(); | ||
|
||
Console.WriteLine($"Context configured for testing with provider: {context.Database.ProviderName}"); | ||
Console.WriteLine("Testing configuration added via extension method"); | ||
Console.WriteLine(); | ||
} | ||
|
||
private static void ConfigurationCompositionExample() | ||
{ | ||
Console.WriteLine("=== Configuration Composition Example ==="); | ||
|
||
#region ConfigurationComposition | ||
var services = new ServiceCollection(); | ||
|
||
// First: add logging | ||
services.ConfigureDbContext<BlogContext>(options => | ||
options.LogTo(Console.WriteLine)); | ||
|
||
// Second: add the provider | ||
services.AddDbContext<BlogContext>(options => | ||
options.UseInMemoryDatabase("CompositionExample")); | ||
|
||
// Third: add sensitive data logging | ||
services.ConfigureDbContext<BlogContext>(options => | ||
options.EnableSensitiveDataLogging()); | ||
|
||
var serviceProvider = services.BuildServiceProvider(); | ||
#endregion | ||
AndriySvyryd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
using var scope = serviceProvider.CreateScope(); | ||
var context = scope.ServiceProvider.GetRequiredService<BlogContext>(); | ||
|
||
Console.WriteLine($"Context configured with provider: {context.Database.ProviderName}"); | ||
Console.WriteLine("All three configurations were composed together"); | ||
Console.WriteLine(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.