-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Add a static flag in EF that will be set when code is being executed for design-time discovery #27306
Comments
I have already used |
@hookenful it's difficult to understand from the above what exactly is going on, as a lot of details are missing. Are you saying that when executing |
This can happen now--see dotnet/runtime#53757 This issue is lacking enough information for us to be able to fully understand what is happening. Please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate. |
@roji is right. I am executing command in Developer powershell in VS2019. And my app is starting after this. dotnet ef migrations add VisibleLink -p .\src\Only.Portal.Data\ -s .\src\Only.Portal.Web |
Which is causing some problem, because for example if I need to revert migration ->
"The migration '20220128090939_VisibleLink3' has already been applied to the database. Revert it and try again. If the migration has been applied to other databases, consider reverting its changes using a new migration instead.." Somehow dead end loop. |
There are 2 questions dated earlier than 3 months both about this behavior: https://stackoverflow.com/questions/70368401/why-does-dotnet-ef-migrations-add-start-my-application |
I'm facing this issue too when creating the .net6 project and using startup.cs file like .net 5. after few days reading again on how to upgrade from .net5 to .net 6 I'm implementing "Use Startup with the new minimal hosting mode" and executing "add-migration" my code runs well. Hope it helps. |
@frogerdevs for sure it helped, thanks a lot :) good luck for all you blessed guys. |
Notes from triage: the issue here is that, in order to support minimal APIs, a hosted application is now executed in order to discover services. This is a problem if the application performs migrations (or does anything else that is incompatible with design-time) before the host is terminated. In 7.0, we plan to add a static "design-time" flag to EF Core that can be checked while the application is running. This flag can be used by the application to avoid applying migrations or doing anything else that should not happen at startup. This issue will track that change. For now, the best workaround is to use a Startup class, as suggested above by @frogerdevs . See Use Startup with the new minimal hosting model for more information. In addition, we will look into creating an IDesignTimeContextFactory without first doing full DbContext type discovery. /cc @davidfowl |
I wonder if we could make this more generic and not have it be an EF static but a static that any tool can use? We can base it on an environment variable or app context switch or something like that as well so we could potentially backport it to 6.0 |
@davidfowl Can you suggest a place to put it? |
The EF-specific flag could be the version of the tool, so when invoked by a different major version than expected a (potentially third-party) service could display a warning |
@danroth27 Brought up an interesting point today, if you were creating the database in Program.Main before, there's currently no way to do that with a Minimal API project that works well with dotnet-ef. Here's the old pattern: public class Program
{
public static void Main(string[] args)
{
var host = CreateHostBuilder(args).Build();
// Migrate
using (var scope = host.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
db.Database.Migrate();
}
host.Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
} And here's what it might look like as Minimal API: var builder = WebApplication.CreateBuilder(args);
var startup = new Startup(builder.Configuration);
startup.ConfigureServices(builder.Services);
var app = builder.Build();
// TODO: Need a flag to skip during dotnet-ef
//if (!EF.IsInDesignMode)
//{
// Migrate
using (var scope = app.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
db.Database.Migrate();
}
//}
startup.Configure(app, app.Environment);
app.Run(); |
I'm using this little trick. It works for my scenarios. public static bool IsDesignTime()
{
var args = Environment.GetCommandLineArgs();
if (args.Length < 1)
{
return false;
}
var arg = args[0];
return Path.GetFileName(arg) == "ef.dll";
} |
I would be nice to update documentation at https://docs.microsoft.com/en-us/ef/core/cli/dbcontext-creation?tabs=dotnet-core-cli#from-a-design-time-factory if design time factory is not supported anymore in .net 6. |
…for design-time discovery Fixes #27306.
…for design-time discovery Fixes #27306.
…for design-time discovery Fixes #27306.
I have strange behavior with my EF Core tools in developer powershell in VS2019.
I am creating migration with dotnet ef migrations add VisibleLink3 -p .\src\Only.Portal.Data\ -s .\src\Only.Portal.Web command.
And it is starting my app, but previously it didn't.method to apply last migrations. Which causes dotnet ef migrations remove fully broken, because when using it, it firstly start app and Migrate().
Then I have a message:
The migration '20220128090939_VisibleLink3' has already been applied to the database. Revert it and try again. If the migration has been applied to other databases, consider reverting its changes using a new migration instead..
Looks like dead end loop.
The text was updated successfully, but these errors were encountered: