Your startup project doesn't reference Microsoft.EntityFrameworkCore.Design

后端 未结 15 780
天涯浪人
天涯浪人 2021-02-01 12:51

I have 2 projects in my solution, I have a project with Entity Framework Core installed:

And in the other ASP.NET Web API project I have these packages:

15条回答
  •  佛祖请我去吃肉
    2021-02-01 13:21

    If you want to do migrations without adding EF references in your services, your library with data access needs a way to construct an instance of your DbContext. You can do this by implementing IDesignTimeDbContextFactory, example:

    public class MyContextFactory : IDesignTimeDbContextFactory
    {
        public MyContext CreateDbContext(string[] args)
        {
            var optionsBuilder = new DbContextOptionsBuilder();
            optionsBuilder.UseSqlServer("Data Source=MyDatabase");
    
            return new MyContext(optionsBuilder.Options);
        }
    }
    

提交回复
热议问题