如何使用C#在ASP.NET Core应用程序中纠正错误。

huangapple go评论72阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年5月22日 16:40:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76304398.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定