Ashkan
Ashkan

Reputation: 89

ASP.NET Core Razor pages: error format of the initialization string does not conform to specification starting at index 0

I am programming an ASP.NET Core project with Razor pages, which is database first.

After executing the Scaffold-DbContext command in Power Console Manager, the classes and context were created correctly and after running the program, the data was read correctly from the tables. Then I moved the connection string from context to appsettings and also the name of the connection string to the context and replaced the connection string, and after this step everything still worked properly.

Now after the changes I made in the tables through SSMS, I again run Scaffold-Dbcontext in order to receive the changes of the tables and I changed the connection string name to context again, but this time after running the program and while reading data from the tables, the following I get this error :

System.ArgumentException

HResult=0x80070057

Message=Format of the initialization string does not conform to specification starting at index 0.

Summary: When the connection string is in context (like the default state), it works correctly. But when I save the name of the connection string in the context, it encounters a data call error

I have attached pictures of the current situation, please take a look and tell me where I am doing wrong. 🤔

enter image description here

Upvotes: 0

Views: 88

Answers (1)

Yuning Duan
Yuning Duan

Reputation: 1692

System.ArgumentException

HResult=0x80070057

Message=Format of the initialization string does not conform to specification starting at index 0.

This error usually occurs because the format of the initialization string does not conform to the specification.In your method optionsBuilder.UseSqlServer("connectionstring"); ,directly pass "connectionstring" as the connection string to the UseSqlServer method, so an error will occur. I reproduced your error:

enter image description here

Solution:

We can inject the IConfiguration object into the DBContext constructor and then use the GetConnectionString method of IConfiguration to get the connection string. Here is an example you can refer to:

public class WebApplication11Context : DbContext
{
    private readonly IConfiguration _configuration;
    public WebApplication11Context(IConfiguration configuration)
     : base(GetOptions(configuration))
    {
        _configuration = configuration;
    }

    public DbSet<Movie> Movie { get; set; } = default!;

    private static DbContextOptions GetOptions(IConfiguration configuration)
    {
        var optionsBuilder = new DbContextOptionsBuilder<WebApplication11Context>();

        optionsBuilder.UseSqlServer(configuration.GetConnectionString("MyConnectionString"));
        return optionsBuilder.Options;
    }
}

Appsetting:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "MyConnectionString": "Server=(localdb)\\mssqllocaldb;Database=WebApplication11Context-2372467b-a09c-4c4c-a4c1-3a81bbb6f823;Trusted_Connection=True;MultipleActiveResultSets=true"
  }
}

enter image description here

Upvotes: 0

Related Questions