Can not make a Entity code-first migration at designtime

Jørgen Vig Jensen 0 Reputation points
2025-10-16T20:58:51.3033333+00:00

I would like to run add-migration at design-time on a simple C# application.

Model:

    [Table("Feature")]
    public class Feature
    {
        public int Id { get; set; }
        public string? Priceclass { get; set; }
        public decimal Price { get; set; }
    }

DbContext:

    public class LivfakDbContext : DbContext
    {
        internal DbSet<Feature> Features { get; set; }
        public LivfakDbContext(DbContextOptions<LivfakDbContext> options) : base(options)
        {
        }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlite("Filename=livfakDB.db");
        }
        }
    }

IDesignTimeDbContextFactory:

    public class LivfakDbContextFactory : IDesignTimeDbContextFactory<LivfakDbContext>
    {

        LivfakDbContext IDesignTimeDbContextFactory<LivfakDbContext>.CreateDbContext(string[] args)
        {
            var optionsBuilder = new DbContextOptionsBuilder<LivfakDbContext>();
            optionsBuilder.UseSqlite("Filename=livfakDB.db");

            return new LivfakDbContext(optionsBuilder.Options);
        }

    }

When I run the add-migration command., it throws this exception:

Unable to create a 'DbContext' of type 'RuntimeType'. The exception 'The type initializer for '<Module>' threw an exception.' was thrown while attempting to create an instance.

As I can read, I have to use the IDesignTimeDbContextFactory when I want to migrate at design-time. But I cant see what I miss.

Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Starry Night 600 Reputation points
    2025-10-17T03:06:52.4066667+00:00

    The occurrence of this kind of problem typically indicates an issue with the configuration or registration of your DbContext in Entity Framework Core.

    Example

    public class MyDbContext : DbContext
    {
       public MyDbContext(DbContextOptions<MyDbContext> options) : base(options) { }
       public MyDbContext() { }
       public DbSet<User> Users { get; set; }
    }
    

    Common Causes and Solutions

    1. Missing Parameterless Constructor

    Ensure your DbContext class has a parameterless constructor.

    public MyDbContext() { }
    

    2. Incorrect Service Registration

    Make sure your DbContext is correctly registered in the Startup.cs or Program.cs.

    var builder = WebApplication.CreateBuilder(args);
    builder.Services.AddDbContext<MyDbContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
    

    3. Multiple Startup Projects

    If you have multiple startup projects, specify the correct project when running migrations.

    dotnet ef migrations add InitialCreate --project YourDbContextProject --startup-project YourStartupProject
    

    4. Using IDesignTimeDbContextFactory

    Implementing IDesignTimeDbContextFactory can help resolve design-time issues.

    public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<MyDbContext>
    {
       public MyDbContext CreateDbContext(string[] args)
      {
         var optionsBuilder = new DbContextOptionsBuilder<MyDbContext>();
         optionsBuilder.UseSqlServer("YourConnectionString");
     
         return new MyDbContext(optionsBuilder.Options);
      }
    }
    

    By following these steps, you can resolve the issue and successfully create and use your DbContext in Entity Framework Core


  2. Varsha Dundigalla(INFOSYS LIMITED) 2,700 Reputation points Microsoft External Staff
    2025-10-22T11:15:45.69+00:00

    Thank you for reaching out.

    This happens because WinUI packaged apps use MSIX tooling, which enforces platform-specific build requirements. When you run dotnet ef migrations add, EF Core tries to build the startup project to locate your DbContext. That build triggers MSIX packaging targets, which expect a defined RuntimeIdentifier and platform (like x64 or ARM64) and do not allow AnyCPU. The EF CLI doesn’t provide those settings during design-time, so the build fails. Essentially, EF migrations assume a normal .NET project, but MSIX adds extra constraints that break the process.

    Create a Separate Migration Project (Recommended)

    1. Add a new Class Library or Console App to your solution.
    2. Move your DbContext and IDesignTimeDbContextFactory into this project.
    3. Install EF Core packages (Microsoft.EntityFrameworkCore, Microsoft.EntityFrameworkCore.Sqlite).
    4. Run:dotnet ef migrations add InitialCreate --project YourEfProject --startup-project YourEfProject
    5. Reference this EF project in your WinUI app.

    Temporarily Disable MSIX Tooling

    1. Edit your WinUI .csproj and set:<EnableMsixTooling>false</EnableMsixTooling>
    2. Run: dotnet ef migrations add InitialCreate
    3. Re-enable MSIX tooling after migration.

    Use --no-build

    1. Build your project manually first: dotnet build
    2. Run migration without building: dotnet ef migrations add InitialCreate --no-build

    Specify Runtime Identifier

    1. Add this to .csproj: <RuntimeIdentifiers>win-x64</RuntimeIdentifiers>
    2. Run: dotnet ef migrations add InitialCreate --runtime win-x64

    Please let us know if you require any further assistance we’re happy to help.

    If you found this information useful, kindly mark this as "Accept Answer".


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.