在迁移添加时创建我的上下文的问题

problem with creating my Context on migration adding


我已经在项目上工作了一段时间,我能够毫无问题地添加迁移,但是一两周前我从使用
onconfiguring optionsbuilder.useSqlServer 过渡到从我的上下文 class 连接到 sql 服务器到使用 Startup class 中的服务连接到数据库
当我尝试 运行 添加迁移时出现此错误
无法创建 'mojDbContext' 类型的对象。对于设计时支持的不同模式

计划class

 public class Program
{
   
    
    
    
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
}

启动 class
 public class Startup
{
    public IConfiguration Configuration { get; }
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

   

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<mojDbContext>(options=> options.UseSqlServer(Configuration.GetConnectionString("ererer")));
        services.AddControllersWithViews();

        services.AddMvc();
        services.AddDistributedMemoryCache();
        services.AddSession();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }
        app.UseSession();
        app.UseHttpsRedirection();
        app.UseStaticFiles();

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
        });
    }
}


我的上下文 class 删除了 dbset 属性以保持代码更小

 public class mojDbContext : DbContext
{
    

   
    public mojDbContext(DbContextOptions<mojDbContext> options)
        : base(options)
    {
    }



}

当我 运行 add-migration -verbose 我得到这个

https://i.stack.imgur.com/Rn7Op.png

您需要添加 dbset tour dbcontext

     public class mojDbContext : DbContext
{
    

   
    public mojDbContext(DbContextOptions<mojDbContext> options)
        : base(options)
    {
    }

   public DbSet<YourTable> YourTable{ get; set; }

}

您可以通过实现 IDesignTimeDbContextFactory 接口告诉迁移如何创建 DbContext:如果在与派生 DbContext 相同的项目或应用程序的启动项目中找到实现此接口的 class,则工具绕过其他创建 DbContext 的方法,而是使用 design-time 工厂。

https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dbcontext-creation