-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Add API to remove provider configuration. #35126
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
Comments
Not sure how or why, but removing the
|
@cincuranet: closing this feels a bit wrong. The documentation like: https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-9.0#customize-webapplicationfactory will give the error I described? And is my solution the right way to do this? Or is there a better way? |
I'm experiencing this same exact issue. In my case, I'm using .NET 8 but have some EF-related packages -- such as EF core -- that I've updated to v9.0.0. Can this be re-opened, @cincuranet? If not, @Tealons perhaps we should create a new thread for this issue? I would also like to understand if there's been an intentional change in behavior.
|
You don't want to mix multiple providers with single I guess you're doing testing. It would be way better to have setup for tests that registers (only) your desired provider and setup for production. Using methods like |
@cincuranet I'm concerned about this one, since there is an indication that this is a regression. @Tealons @kymarti Can one of you please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate. |
Sure, let me try to put something together @ajcvickers , but if you have time @Tealons feel free to go ahead and put something together as well. |
@ajcvickers If you downgrade all packages to .net 8 the tests will succeed, but with .net 9 this exception occurs.
|
Amazing, thanks @loekensgard ! I didn't get a chance to put a sample project together yesterday. |
Just run into this trying to update from 8 to 9: 8 works, 9 doesn't. So I guess we're staying with 8. |
Note for team: I am able to reproduce this and I am investigating. It looks like a regression in ASP.NET testing. |
Looks like this is a change in the behavior public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<ContextOfDoom>(options => options.UseSqlServer());
builder.Build().Run();
}
}
public class ContextOfDoom : DbContext
{
public ContextOfDoom(DbContextOptions<ContextOfDoom> options) : base(options) { }
} And then a test project with a test and a public class TestWebApplicationFactory : WebApplicationFactory<Program>
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureServices(services =>
{
services.AddDbContext<ContextOfDoom>((_, context) => context.UseInMemoryDatabase("DB"));
var serviceProvider = services.BuildServiceProvider();
using var scope = serviceProvider.CreateScope();
var context = serviceProvider.GetService<ContextOfDoom>()!;
_ = context.Model;
});
}
}
public class SomeTests : IClassFixture<TestWebApplicationFactory>
{
public SomeTests(TestWebApplicationFactory factory)
{
factory.WithWebHostBuilder(_ => { }).CreateClient();
}
[Fact]
public async Task Get_Test()
{
}
} When running the tests in EF8, the web host resolver is not used, and so that Main method is not run, and only the in-memory database is registered. When running the tests in EF9, the web host resolver runs and so both the SQL Server and in-memory databases are registered, resulting in:
|
@AndriySvyryd Assigning to you since you did some work in this area in EF8/9. Root cause could be an external breaking change in the |
I usually remove the current registered service: var descriptor = services.SingleOrDefault(d => d.ServiceType == typeof(DbContextOptions<WfEksternDbContext>));
if (descriptor != null)
{
services.Remove(descriptor);
} |
@ErikEJ, it appears that this issue persists even after removing the currently registered service. In my test project, I have the same configuration, but the error still occurs when running .NET 9. However @Tealons found a workaround, but this could potentially lead to unexpected behavior?
|
@ErikEJ Yeah, I saw that in one of the repros--doesn't help in 9. |
Seeing the same issue, tagging myself in for updates. |
I ran into the same issue after upgrading from EF8 to 9. My Startup. cs has four DI DBContexts, so I made sure to remove all four, even though my tests only use three of them.
to
My tests are running again. |
Unfortunately the Microsoft staff said this different error message would be tracked here: #35179 And closed that issue. |
I think you've got mixed up: on thread #35179 @ajcvickers says to come to this thread. |
@rwb196884 Yes. That is what I was trying to communicate. |
Yes, @JYasgarYTGI nailed it (my tests pass now, too). But I only have a few basic tests and am wondering if we need to be concerned about side effects of using the IDbContextOptionsConfiguration interface over the DbContextOptions implementation. And it aligns with my discovery that IDbCOntextOptionsConfiguration is "left over" otherwise (as per my testing above). Thanks Jack! |
I have the same problem, for now I will have to stay on .NET8. I tag myself too. |
Sorry that it took so long for the response. This is by design. Starting with 9 The best workaround currently is to remove Since there isn't an API to remove a provider configuration, we can use this issue to track adding it. This is a fairly advanced scenario, so it doesn't need to be easily discoverable. I propose the following: services.ConfigureDbContext<TestContext>(options =>
((IDbContextOptionsBuilderInfrastructure)options).RemoveExtension<SqlServerOptionsExtension>()); |
@AndriySvyryd if it is by design, surely this should be edited in the documentation of learn.microsoft .NET 9 ASAP? Anyone currently following the documentation, will hit a very unpleasant surprise "I did exactly as the docs said... WHY DOESN'T IT WORK?" |
@BlNARYFOUR Thanks for pointing this out. Filed dotnet/AspNetCore.Docs#34584 Once the specialized API is added https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests#customize-webapplicationfactory should be updated |
I could not figure out how to solve it from the comments, but this blog post helped med out. |
Taking my post above, I wrote it up too. https://jackyasgar.net/solved-ef-8-to-9-migration-database-provider-exception/ |
I didn't know that link building was still a thing :) |
When this is a breaking change, version 9 of EF Core should be limited to .net 9 - we have applications on LTS (.net 8) failing because of this. |
@adcorduneanu You can use EF Core 8 on .net 8 |
@AndriySvyryd, this is not an option when you have over 150 projects in the field, and the updates are done automatically. If this was targeting .net 9 then should be only on .net 9. |
Well that gets the tests to run but they're failing. Seems that navigation properties aren't being saved.
|
I'll just comment it out. Unit tests are worthless when you have to spend more time fucking about to fix them then on real code. |
…textOptionsConfiguration (source: dotnet/efcore#35126 (comment)). [+semver: patch]
…textOptionsConfiguration (source: dotnet/efcore#35126 (comment)). [+semver: patch]
After upgrading to .NET 9 I get the following error on
db.Database.EnsureCreated();
in the following code:The exact error:
My feeling is that removing the previous ApplicationDbContext is not working properly? This is my code (which was adjusted according to the .NET 9 docs, but is still not working):
After hours of Googling and trying stuff out I'm a bit lost how this should work in .NET 9...
The text was updated successfully, but these errors were encountered: