英文:
How to Correct Error in ASP.NET Core Application with C#
问题
I got this error "初始化字符串的格式不符合从索引0开始的规范。" 在迁移过程中。
我的连接字符串在
appsettings.json
"ConnectionStrings": {
"DefaultConnection": "Data Source=LAPTOP-P08F3TG1/ckary;Initial Catalog=Product;Integrated Security=True"
},
ApplicationDbcontext.cs
public class ApplicationDbContext:DbContext
{
public DbSet<Login> Logins { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("DefaultConnection");
}
请帮助找出我的错误
正确答案或解决我的错误的格式
英文:
I got this error "Format of the initialization string does not conform to specification starting at index 0."
while migrating.
my connection string given in
appsettings.json
"ConnectionStrings": {
"DefaultConnection": "Data Source=LAPTOP-P08F3TG1/ckary;Initial Catalog=Product;Integrated Security=True"
},
ApplicationDbcontext.cs
public class ApplicationDbContext:DbContext
{
public DbSet<Login> Logins { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("DefaultConnection");
}
please help to findout my mistake
correct answer or format to solve my error
答案1
得分: 1
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
IConfigurationRoot configuration = new ConfigurationBuilder()
.SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
.AddJsonFile("appsettings.json")
.Build();
optionsBuilder.UseSqlServer(configuration.GetConnectionString("DefaultConnection"));
}
英文:
Try:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
IConfigurationRoot configuration = new ConfigurationBuilder()
.SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
.AddJsonFile("appsettings.json")
.Build();
optionsBuilder.UseSqlServer(configuration.GetConnectionString("DefaultConnection"));
}
答案2
得分: 0
"Could you try this?"
"ConnectionStrings": {
"DefaultConnection": "Data Source=LAPTOP-P08F3TG1\ckary;Initial Catalog=Product;Integrated Security=True"
}
英文:
Could you try this?
"ConnectionStrings": {
"DefaultConnection": "Data Source=LAPTOP-P08F3TG1\ckary;Initial
Catalog=Product;Integrated Security=True"
}
答案3
得分: 0
When using JSON based configuration you first need to ensure that your string values are encoded correctly, many examples on connection strings will assume XML based configuration that has different encoding rules.
Specifically, for usernames and data sources (servers) that commonly contain the back-slash character \
, in JSON this is an escape character itself, so we need to escape it like this:
LAPTOP-P08F3TG1\\ckary
"ConnectionStrings": {
"DefaultConnection": "Data Source=LAPTOP-P08F3TG1\\ckary;Initial Catalog=Product;Integrated Security=True"
}
this answer by Quing Guo should help if your appsettings.json
file is not in the default location or your project has been upgraded to ASP.Net Core from a legacy codebase.
英文:
When using JSON based configuration you first need to ensure that your string values are encoded correctly, many examples on connection strings will assume XML based configuration that has different encoding rules.
Specifically, for usernames and data sources (servers) that commonly contain the back-slash character \
, in JSON this is an escape character itself, so we need to escape it like this:
LAPTOP-P08F3TG1\\ckary
"ConnectionStrings": {
"DefaultConnection": "Data Source=LAPTOP-P08F3TG1\\ckary;Initial Catalog=Product;Integrated Security=True"
}
this answer by Quing Guo should help if your appsettings.json
file is not in the default location or your project has been upgraded to ASP.Net Core from a legacy codebase.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论