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
Scaffolding doesn't work when DbContext is in a separate project #1765
Comments
Did you find a solution? I'm having the same issue. The migrations are working fine, but Scaffolding wont work. Only slight difference is I am using IdentityDbContext. |
Same issue here . But with Version 6.02 |
Same here. |
I found a solution. public class ApplicationDbContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext>
{
public ApplicationDbContext CreateDbContext(string[] args)
{
var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
optionsBuilder.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=EcommerceDb;Trusted_Connection=True;MultipleActiveResultSets=true");
return new ApplicationDbContext(optionsBuilder.Options);
}
} IDE: VS 2022 Pro |
Thanks Cem, nice workaround though, its working! |
@cemg Thanks! This also works for me. |
Thanks @cemg. Found this after hours of testing. Works great |
thanks for the solution |
Had the same issue, with scaffolding an api controller. This did the trick, THANKS! |
This solution works, thank you! |
You can use ConfigurationBuilder class for build the configuration. public class ApplicationDbContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext>
{
public ApplicationDbContext CreateDbContext(string[] args)
{
var configuration = new ConfigurationBuilder()
.SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
.AddJsonFile("appsettings.json")
.Build();
var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
optionsBuilder.UseSqlServer(configuration.GetConnectionString("Default"));
return new ApplicationDbContext(optionsBuilder.Options);
}
} |
Although this resolved the issue for me, it introduced some side effects which I only realized weeks later. It changes some of the internals of identity classes, see the following migration:
Even though nothing else was changed in the project (related to the database schema), when your proposed class is present, these columns are changed when trying to create a new migration. Removing the class and rescaffolding the migration results in an empty migration. Setting the Not sure where this comes from but maybe this is useful information to someone. |
Worked on first try. Thanks! |
thanks bro |
Not working at my end getting same issue Microsoft Visual Studio Community 2022 (64-bit) - Preview TargetFramework net7.0 |
@cemg Thanks! This work for me. |
Issue with this approach is that it doesn't consider environment (Development/Staging/Production) for the appsettings.{env}.json. |
It was useful for me but the SetBasePath method throwed me an error. I fix it adding two nugget packages: Microsoft.Extensions.Configuration.FileExtensions The answer was commented on: |
I believe that your problem has come from elsewhere, microsoft itself recommends building a db context factory. |
Thanks. Worked like a charm! |
Does this also apply when referencing a different DBContext? |
Is Also works for me . thx❤️ |
This doesn't work as shown with .NET 7.. You now need to add new package references. See this page - https://learn.microsoft.com/en-us/dotnet/core/extensions/configuration#basic-example |
it works for me in .NET 7 |
Awesome! Thanks cemg. It works for me. |
Hey @cemg Thanks |
Is there a more concrete solution to this? Creating a temporary factory feels pretty janky. It's been several years, lets FIX the tool instead of work around it. |
I have to agree. It's absolutely absurd that a workaround is required for what has to be one of the most common scenarios in using ef core. |
I cannot believe this incredible simple solution is not in the official documentation, @cemg thank you for this!! |
Solution was adding a dbcontextfactory. dotnet/Scaffolding#1765
you saved us cemg I was considering creating everything by hand, but it seems that divine intervention has sent you to help us. Thanks a lot |
Thank you very much, this saved my creation of razor pages with entity framework |
Consistently getting this error when attempting to scaffold a controller while having DbContext in a separate project:
MyProjectDbContext:
DbContext is registered in Program.cs as follows:
Scaffolding works if DbContext moved to the web project.
IDE: Visual Studio 2022 Community
SDK: .NET 6.0
Packages:
"Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.1"
"Microsoft.EntityFrameworkCore.Tools" Version="6.0.1"
"Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.1"
The text was updated successfully, but these errors were encountered: