我有一个.net核心应用程序,我正在尝试与EntityFrameworkCore一起使用来构建数据库,但是当我这样做时:
update-database
出现此错误:
Format of the initialization string does not conform to specification starting at index 0.
我已将连接字符串更改为标准格式,并尝试了此操作,因为我遇到了错误,因为主项目的汇编与我用于类的库不同:
services.AddDbContext<conn>(options => options.UseSqlServer("connname", b => b.MigrationsAssembly("conn")));
"ConnectionStrings": {
"FITMEConnection": "Server=foo;Database=fooname;Trusted_Connection=True"
},
我对此很生气,因为我已经搜索了此错误并且似乎很容易解决。我究竟做错了什么?谢谢。
答案 0 :(得分:1)
首先,您的appsettings.json
文件应如下所示:
{
"ConnectionStrings": {
"FITMEConnection": "Server=foo;Database=foo;Trusted_Connection=True;MultipleActiveResultSets=true"
},
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*"
}
然后执行以下操作:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
var connectionString = Configuration.GetConnectionString("FITMEConnection"); // <-- Look at here
services.AddDbContext<YourDbContext>(options => options.UseSqlServer(connectionString),b => b.MigrationsAssembly("MigrationAssemblyName"));
}
}
现在应该可以了!