-
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 3 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
Some comments aren't visible on the classic Files Changed page.
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="8.0.0" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
121 changes: 121 additions & 0 deletions
121
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,121 @@ | ||
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 | ||
|
||
public class Program | ||
{ | ||
public static void Main(string[] args) | ||
AndriySvyryd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
{ | ||
BasicAddDbContextExample(); | ||
MultipleAddDbContextCallsExample(); | ||
ConditionalConfigurationExample(); | ||
} | ||
|
||
private static void BasicAddDbContextExample() | ||
{ | ||
Console.WriteLine("=== Basic AddDbContext Example ==="); | ||
|
||
#region BasicAddDbContext | ||
var services = new ServiceCollection(); | ||
|
||
// DbContext registration with configuration | ||
services.AddDbContext<BlogContext>(options => | ||
options.UseInMemoryDatabase("BasicExample") | ||
.EnableSensitiveDataLogging() | ||
.EnableDetailedErrors()); | ||
|
||
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 successfully with provider: {context.Database.ProviderName}"); | ||
Console.WriteLine(); | ||
} | ||
|
||
private static void MultipleAddDbContextCallsExample() | ||
{ | ||
Console.WriteLine("=== Multiple AddDbContext Calls Example (EF Core 8.0 Behavior) ==="); | ||
|
||
#region MultipleAddDbContextCalls | ||
var services = new ServiceCollection(); | ||
|
||
// First configuration - will be overridden in EF Core 8.0 | ||
services.AddDbContext<BlogContext>(options => | ||
options.UseInMemoryDatabase("FirstDatabase")); | ||
|
||
// Second configuration - this takes precedence (EF Core 8.0 behavior) | ||
services.AddDbContext<BlogContext>(options => | ||
options.UseInMemoryDatabase("SecondDatabase") | ||
.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($"Provider: {context.Database.ProviderName}"); | ||
Console.WriteLine("Note: In EF Core 8.0, the second AddDbContext call takes precedence over the first one."); | ||
Console.WriteLine("This is different from EF Core 7.0 and earlier where the first call would win."); | ||
Console.WriteLine(); | ||
} | ||
|
||
private static void ConditionalConfigurationExample() | ||
{ | ||
Console.WriteLine("=== Conditional Configuration Example ==="); | ||
|
||
#region ConditionalConfiguration | ||
var services = new ServiceCollection(); | ||
|
||
// Mock environment and configuration services | ||
var isDevelopment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Development"; | ||
|
||
services.AddDbContext<BlogContext>((serviceProvider, options) => | ||
{ | ||
if (isDevelopment) | ||
{ | ||
options.UseInMemoryDatabase("DevelopmentDatabase") | ||
.EnableSensitiveDataLogging() | ||
.EnableDetailedErrors(); | ||
} | ||
else | ||
{ | ||
options.UseInMemoryDatabase("ProductionDatabase"); | ||
} | ||
}); | ||
|
||
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 environment: {(isDevelopment ? "Development" : "Production")}"); | ||
Console.WriteLine($"Provider: {context.Database.ProviderName}"); | ||
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.