添加迁移时,我在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相关问答推荐

.NET 框架 3.5 和 TLS 1.2

带有模型的 mvc 上传文件 - 第二个参数发布的文件为空

系统日期时间?与 System.DateTime

使用会话变量有多安全 - asp.net / c#

有条件地在 Gridview 中隐藏 CommandField 或 ButtonField

是否可以使用 Membership API 更改用户名

将存储过程中 Select 查询的结果返回到列表

ASP.Net Core MVC - 自定义属性的客户端验证

什么是实体框架中的复杂类型以及何时使用它?

ASP.NET IIS Web.config [内部服务器错误]

如何在不预编译的情况下使用命令行msbuild部署VS2012网站项目?

IIS Express 安装目录在哪里?

web.config 中与 targetFramework 相关的配置错误

Fiddler 没有从 ASP.NET 网站嗅探 SOAP 流量

ASP.NET 按钮重定向到另一个页面

如何在 .NET Core 中实现 DbContext 连接字符串?

如何将我的 DDD 模型中的用户与身份验证用户集成?

服务器标签格式不正确错误

如何从 ASP.NET Identity 获取用户列表?

从服务器下载 ASP.NET 文件