Search code examples
c#databasevisual-studiorazor-pages

Update-Database error - Format of the initialization string does not conform to specification starting at index 0


Hi and sorry for my English.

General view:

VS2022 / Razor Project.

Development in a W11Pro machine with local SQL 2019

Production in a W2022Std machine with local IIS and local SQL 2022

Project published via ftp protocol.

Project work very well in both environment. No issue except the below one regarding the update of remote db.

In summary:

Add- Migration work well. Migrations are added and maintained

Update-Database work well. Database in Local SQL in development machine is properly updated and maintained

Update-Database -Connection ProductionConnection (to update the database in Production Server) fail with error:

Build started...

Build succeeded.

System.ArgumentException: Format of the initialization string does not conform to specification starting at index 0.

appsettings.json
{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "DevelopmentConnection": "Server=WKSDEVELOPMENT\\SQLEXPRESS;uid=userdev;pwd=passworddev;Database=DBDevelopment;Encrypt=True;TrustServerCertificate=True;",
    "ProductionConnection": "Server=SRVPRODUCTION\\SQLEXPRESS;uid=userproduction;pwd=passwordproduction;Database=DBProduction;Encrypt=True;TrustServerCertificate=True;"
  }
}

NO Special characters in both password. Only letters and numbers.

Program.cs
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Intranet.Data;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages();

if (builder.Environment.IsDevelopment())
{
    builder.Services.AddDbContext<IntranetContext>(options =>
        options.UseSqlServer(builder.Configuration.GetConnectionString("DevelopmentConnection") ?? throw new InvalidOperationException("Connection string 'DevelopConnection' not found.")));
}
else
{
    builder.Services.AddDbContext<IntranetContext>(options =>
        options.UseSqlServer(builder.Configuration.GetConnectionString("ProductionConnection") ?? throw new InvalidOperationException("Connection string 'ProductionConnection' not found.")));
}

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
}
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapRazorPages();

app.Run();

IntranetContext.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Intranet.Model;
using Intranet.Models;
using Intranet.Models.Tabelle;
using EntityFramework.Exceptions.SqlServer;

namespace Intranet.Data
{
    public class IntranetContext : DbContext
    {
        public IntranetContext(DbContextOptions<IntranetContext> options)
            : base(options)
        {
        }

        public DbSet<Intranet.Model.Customer> Customer { get; set; }

        public DbSet<Intranet.Model.CustomerContacts> CustomerContacts { get; set; }
  }
}

As i said, application work fine in both environments so i'm unable to understand why both connection strings work but are "not good" for the Update-Database routine.

Hope i was clear in my explanation and thanks in advance for any help.

Best regards.

Massimo

Update:

Here what i have with a breakpoint.

enter image description here

To add another bit: Also the command Update-Database -Connection DevelopmentConnection finish with the same error.


Solution

  • Update-Database -Connection ProductionConnection seems to need a valid connection string, not its identifier of the appsettings.json;

    -Connection The connection string to the database. Defaults to the one specified in AddDbContext or OnConfiguring.

    https://learn.microsoft.com/en-us/ef/core/cli/powershell

    So what you'd likely need is to run;

    Update-Database -Connection Server=WKSDEVELOPMENT\\SQLEXPRESS;uid=userdev;pwd=passworddev;Database=DBDevelopment;Encrypt=True;TrustServerCertificate=True; (i feel like you need a single slash here for the server but i'm not sure; WKSDEVELOPMENT\SQLEXPRESS. If that does not work, try wrapping the connecting string in double quotes)

    I can also imagine that running Update-Database -Args '--environment Production' should run out of the box.