How to detect when project is run by Add-Migration

456 Views Asked by At

I have a .net Core 3.1 Web API solution, using EF Core for the database. When I execute Add-Migration to generate database migrations, the tool runs the startup project. I want to skip some code that executes when the startup project starts (it will not impact the migration generation). Is there a way to tell, in the project code, that it has been launched from the EF tool?

2

There are 2 best solutions below

0
On BEST ANSWER

In Entity Framework Core 7, they've added the static flag EF.InDesignTime that you can check to know if you are in design time:

using Microsoft.EntityFrameworkCore;
 
    public static void Main(string[] args)
    {
        if (EF.DesignTime)
          return;

        // other stuff
    }
0
On

While not completly reliable, a workarround could be to use #if DEBUG directive :

    public static void Main(string[] args)
    {
        #if DEBUG
        // Debug mode: stop here
        return;
        #endif

        // other stuff
    }