error format of the initialization string does not conform to specification starting at index 0

Ashkan 21 Reputation points
2024-07-02T02:55:47.64+00:00

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 (Please see the pictures)

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 (and connection string is in appsettings ), it encounters a data call error

xehvPciI

8yb3ywTK

MBQn0RMp

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,361 questions
0 comments No comments
{count} votes

Accepted answer
  1. SurferOnWww 2,406 Reputation points
    2024-07-02T03:44:47.9533333+00:00

    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 (and connection string is in appsettings ), it encounters a data call error

    You will have to use the DI function so that the instance of context class is automatically injected to the Razor Page.

    (1) Delete (or comment out) the OnConfiguring method in the DbkabirContext class.

    (2) Add the DbkabirContext to the DI container in the Program.cs, as follows:

    namespace RazorPages1
    {
        public class Program
        {
            public static void Main(string[] args)
            {
                var builder = WebApplication.CreateBuilder(args);
                
                // Add following to enable DI
                builder.Services.AddDbContext<DbkabirContext>(options =>
                    options.UseSqlServer(builder.Configuration
                           .GetConnectionString("DbkabirConnectionString")));
    

    (3) Add private readonly DbkabirContext _context; field to the cshtml.cs file and modify the constructor to have argument DbkabirContext context, as follow:

    public class IndexModel : PageModel
    {
        private readonly DbkabirContext _context;
    
        public IndexModel(DbkabirContext context)
        {
            _context = context;
        }
    
        public void OnGet()
        {
            Users = _context.Users.Select( ...
               ...
        }
    }
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful