【发布时间】:2019-01-07 08:46:51
【问题描述】:
我正在尝试使用 Blazor 的 CRUD 功能并遵循一些文章来做到这一点。在文章中,有一部分我应该将我的连接放在上下文文件中,但它没有说明如何设置连接字符串。
我把这行代码放在launchSettings.json中:
{
"ConnectionStrings": {
"UserDatabase": "Server=DESKTOP-2K2A6GN;Database=Assignment4;Trusted_Connection=True;"
},
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:56244/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Assignment4.Server": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:56248/"
}
}
}
并且我尝试将连接字符串添加到上下文文件中,但没有成功。
public class UserContext : DbContext
{
public virtual DbSet<User> tblUser { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer(@"UserDatabase");
}
}
}
【问题讨论】:
-
您应该在 appsetting.json 中添加连接字符串设置
-
但是项目中没有 appsettings.json 文件。这就是我放入launchsettings.json 的原因。那么在哪里可以创建 appsettings.json 文件并连接到项目呢?
标签: c# asp.net-core connection-string