添加迁移时,我在package manager控制台中出现以下错误

值不能为空.参数名称:connectionString

这是我的创业公司:

namespace MyProject
{
    public class Startup
    {
        public IConfiguration Configuration { get; set; }
        public Startup(IConfiguration config)
        {
            Configuration = config;
        }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContextPool<AppDbContext>(options =>
                             options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
            services.AddTransient<IDevRepo, DevRepo>();
            services.AddMvc();
            services.AddMemoryCache();
            services.AddSession();
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseStatusCodePages();
            app.UseStaticFiles();
            app.UseMvcWithDefaultRoute();
            app.Run(async (context) =>
            {
                await context.Response.WriteAsync(Configuration["Message"]);
            });
        }
    }
}

课程类别:

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

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .ConfigureAppConfiguration((context, builder) => builder.SetBasePath(context.HostingEnvironment.ContentRootPath)
                       .AddJsonFile("appsettings.json")
                       .Build())

            .UseStartup<Startup>()
            .Build();
}

应用设置.json:

{
  "Message": "Hello World",
  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\MSSQLLocalDB;Database=NotMyFault;Trusted_Connection=True;MultipleActiveResultSets=true"
  }
}

有趣的是,如果我运行这个应用程序,它会显示"Hello World",但是当添加迁移时,它找不到connectionString.有没有人能在这里开几盏灯?谢谢.

推荐答案

我发现了自己的问题.

我有一个AppDbContextFactory类,它继承了IDesignTimeDbContextFactory.删除此类可以解决此问题.

Asp.net相关问答推荐

将 Response.Headers.Add 替换为方法属性decorator

WebResource.axd 上的填充无效且无法删除异常

在 lambda 表达式中否定 Func

多个 DataContext 类是否合适?

在上下文中找不到 owin.Environment 项

__doPostBack 未定义

如何在 ASP.NET Web API 中使用非线程安全的 async/await API 和模式?

如何在 Visual Studio 2017 中使用 NPM 并安装包?

您对 Windows Workflow Foundation 有何体验?

如何使用自动生成的列隐藏 ASP.NET GridView 中的列?

如何设置 CSS 切换器

Asp.net 中的下拉列表验证使用必填字段验证器

反序列化客户端 AJAX JSON 日期

WebForm_PostBackOptions 文档

由 UpdatePanel 内 GridView 内的 LinkBut​​ton 触发的完整回发

IIS 8.0 ASP.NET 和错误 500.19

如何告诉 RadioButtonList 不生成表格

根据条件更改 GridView 行 colored颜色

StyleBundle 索引超出了数组的范围

Ashx 文件中的 HttpContext.Current.Session 为空