https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-strings
?
我跟随这个站点在json文件中放置了一个连接字符串,但当我尝试访问数据库时,如果我想获取所有酒店的列表,我会得到一个异常“Value cannot be null”
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("ConnectionServerJson.json")
.AddEnvironmentVariables();
configuration = builder.Build();
}
public IConfiguration configuration { get; set; }
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<DBRezervareHotelieraContext>(options =>
options.UseSqlServer(configuration.GetConnectionString("DefaultConnection")));
services.AddScoped<DbContext>(sp => sp.GetService<DBRezervareHotelieraContext>());
var container = services.BuildServiceProvider();
services.AddMvc();
}
这是connectionStringJson。json文件
{
"connectionString": {
"DefaultConnection": "Server=DESKTOP-TPPITPA\\SQLEXPRESS;Database=DBRezervareHoteliera;Trusted_Connection=True;"
}
}
我也从上下文类中删除了
DBRezervareHotelieraContext
这个
onConfigure
方法并放置以下内容
平民的
DBRezervareHotelieraContext(DbContextOptions<DBRezervareHotelieraContext> options) : base(options)
{
}
我不认为这件事,但可能会有帮助