Closed AndyBoot closed 10 months ago
Hi there @AndyBoot!
Firstly, a big thank you for raising this issue. Every piece of feedback we receive helps us to make Umbraco better.
We really appreciate your patience while we wait for our team to have a look at this but we wanted to let you know that we see this and share with you the plan for what comes next.
We wish we could work with everyone directly and assess your issue immediately but we're in the fortunate position of having lots of contributions to work with and only a few humans who are able to do it. We are making progress though and in the meantime, we will keep you in the loop and let you know when we have any questions.
Thanks, from your friendly Umbraco GitHub bot :robot: :slightly_smiling_face:
This could be related to this PR which made it to the 12.2 release: https://github.com/umbraco/Umbraco-CMS/pull/14672
Cc: @nikcio @Zeegaan
Hey @AndyBoot I don't think the PR you referenced above is the culprit but it might be this one instead: #14674
It was meant to make the DbContexts reactive to when a connection string changes like what happens on the Umbraco install and it might have broken registering custom DbContexts in the process.
We might need to figure out a different way to make sure that the connection string and provider is set correctly after an Umbraco install.
@bergmania asked me about a bit similar question not so long ago. Did you add the solution to the code base @bergmania? (The one with the custom DbContext pool)
Then we could test if we need to have the connection string and provider set on the constructor of the DbContext like was done in #14674 or setting it in the service collection extension is reactive when the connection string changes if that makes any sense.
Hey @AndyBoot I don't think the PR you referenced above is the culprit but it might be this one instead: #14674
Ah my bad, I do apologies. Thankfully you knew where else to look though!
Let me know if I can help in anyway, such as testing.
Yes, its merged here https://github.com/umbraco/Umbraco-CMS/pull/14852
Working on another 12.2 project with the same issue. Thought I'd just share my workaround until this is patched.
Ensure the site isn't running via IIS Express or any other instance.
Forcefully revert your project(s) back to 12.1.2 using Find and Replace (be sure to 'Save All' files afterwards):
Generate your migration as before: dotnet ef migrations add MyMigration
Manually run your dotnet ef database update
command. Note that notification handler based approach will not also work under 12.2 as shown here: https://docs.umbraco.com/umbraco-cms/tutorials/getting-started-with-entity-framework-core#step-4-create-the-notification-handler
If you see the following output, then success it worked.
Build started...
Build succeeded.
Done.
Then re-do the Find & Replace, but replace 12.1.2 with 12.2.0 instead.
Build & run your site. As we've not actually run the site during the temporary downgrade you shouldn't hit any issues.
Hopefully that helps somebody during this period 😀
Not sure if this helps someone or not, or even if the problem is the same: I did not have this error during migrations generation, but it happened whenever there was a request to my custom EF Context (I also use v12.2.0 (and updated from 12.1.1).
What I did is I used a different overload to register an ef core context:
Before:
builder.Services.AddUmbracoEFCoreContext<BlogContext>(connectionString, "Microsoft.Data.SqlClient");
After:
builder.Services.AddUmbracoEFCoreContext<BlogContext>(
(options, connectionString, providerName) => options.UseSqlServer(connectionString));
And it started working without issues. Hope it helps!
@AleksandrRogov I was about to mention the same 😅 You can also use this approch if you are using SQLlite:
services.AddUmbracoEFCoreContext<TContext>("connectionString", "provider", (options, connectionString, provider) =>
{
// For SQL Lite
options.UseSqlite(connectionString);
// For SQL Server
options.UseSqlServer(connectionString);
});
Thanks @AleksandrRogov & @nikcio, I can confirm that's fixed my issue.
If this fix is the intended configuration of a project going forward, is it worth an update to the previously linked documentation (https://docs.umbraco.com/umbraco-cms/tutorials/getting-started-with-entity-framework-core) to prevent others from running into the same issue, or could there be a fix implemented in a future hotfix to accomodate for both types of code based configuration?
Which Umbraco version are you using? (Please write the exact version, example: 10.1.0)
12.2
Bug summary
Upon implementing Entity Framework Core on Umbraco 12.2 as per the documentation (https://docs.umbraco.com/umbraco-cms/tutorials/getting-started-with-entity-framework-core) I'm receiving a DbContext error upon running a first migration. I've attempted the same thing on Umbraco 12.1.2 without fail.
Specifics
No response
Steps to reproduce
startup.cs
instead:In a CLI, from the solution root run the following:
dotnet ef migrations add InitialCreate --context BlogContext --project Website -v
Result:
Expected result / actual result
With the same code and same installation steps using Umbraco 12.1.2, we have success: