Search code examples
entity-framework-coreentity-framework-core-migrations

How to detect when project is run by Add-Migration


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?


Solution

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

    using Microsoft.EntityFrameworkCore;
     
        public static void Main(string[] args)
        {
            if (EF.IsDesignTime)
              return;
    
            // other stuff
        }