【发布时间】:2018-02-11 02:17:03
【问题描述】:
添加迁移时,包管理器控制台出现以下错误
值不能为空。参数名称:连接字符串
这是我的创业公司:
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();
}
appsettings.json:
{
"Message": "Hello World",
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\MSSQLLocalDB;Database=NotMyFault;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}
有趣的是,如果我运行该应用程序,它会显示“Hello World”,但是在添加迁移时它找不到连接字符串。有人可以在这里点亮一些灯吗?谢谢。
【问题讨论】:
-
AppDbContext 的构造函数是什么样的?见docs.microsoft.com/en-us/ef/core/miscellaneous/…。您不应使用无参数构造函数,而应使用带有 DbContextOptions 的默认构造函数。
-
@zuckerthoben 谢谢,这就是我使用的。 'public AppDbContext(DbContextOptions
options) : base(options) { }"
标签: asp.net asp.net-mvc asp.net-core