-
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 all 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
102 changes: 102 additions & 0 deletions
102
samples/core/Miscellaneous/ConfiguringDbContext/ConfigureDbContextSample.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,102 @@ | ||
#nullable enable | ||
|
||
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; } | ||
AndriySvyryd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
public static class ConfigureDbContextSample | ||
{ | ||
public static void Run() | ||
{ | ||
BasicConfigureDbContextExample(); | ||
ProviderSpecificConfigurationExample(); | ||
ConfigurationCompositionExample(); | ||
} | ||
|
||
private static void BasicConfigureDbContextExample() | ||
{ | ||
Console.WriteLine("=== Basic ConfigureDbContext Example ==="); | ||
|
||
#region BasicConfigureDbContext | ||
var services = new ServiceCollection(); | ||
|
||
services.ConfigureDbContext<BlogContext>(options => | ||
options.EnableSensitiveDataLogging() | ||
.EnableDetailedErrors()); | ||
|
||
services.AddDbContext<BlogContext>(options => | ||
options.UseInMemoryDatabase("BasicExample")); | ||
#endregion | ||
|
||
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 => | ||
options.UseSqlServer(sqlOptions => | ||
sqlOptions.EnableRetryOnFailure())); | ||
|
||
services.AddDbContext<BlogContext>(options => | ||
options.UseSqlServer("connectionString")); | ||
#endregion | ||
|
||
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 => | ||
options.LogTo(Console.WriteLine)); | ||
|
||
services.AddDbContext<BlogContext>(options => | ||
options.UseInMemoryDatabase("CompositionExample")); | ||
|
||
services.ConfigureDbContext<BlogContext>(options => | ||
AndriySvyryd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
options.EnableSensitiveDataLogging()); | ||
#endregion | ||
|
||
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(); | ||
} | ||
} |
13 changes: 8 additions & 5 deletions
13
samples/core/Miscellaneous/ConfiguringDbContext/ConfiguringDbContext.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 |
---|---|---|
@@ -1,16 +1,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<TargetFramework>net9.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<RootNamespace /> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="8.0.0" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.0" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="8.0.0" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.0" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="9.0.0" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="9.0.0" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="9.0.0" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.0" /> | ||
AndriySvyryd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<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> |
Oops, something went wrong.
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.