question

JORGEMALDONADOBARRERA-7687 avatar image
0 Votes"
JORGEMALDONADOBARRERA-7687 asked JORGEMALDONADOBARRERA-7687 answered

Error in update-database command

Hi,

I am developing an ASP.NET 6 website with VS2022 that includes Identity and I am trying to create the Indentity Tables. The project has 2 contexts for 2 different DBs. I already successfully ran the add-migration command for each context but I am getting an error when issuing the update-database command.

The add-migration command for each context was run as follows:

- add-migration -Name CreateIdentitySchema -OutptDir Data\Migrations\Elisur -Context ElisurDbContext
- add-migration -Name CreateIdentitySchema -OutptDir Data\Migrations\RioPoderoso -Context RioPoderosoDbContext

Both commands ran correctly.

I am trying to run the first update-database command on the ElisurDbContext context as follows:

- update-database -Connection ElisurConnection -Context ElisurDbContext

But I get an error that starts as follows:

Entity Framework Core 6.0.2 initialized 'ElisurDbContext' using provider 'Devart.Data.PostgreSql.Entity.EFCore:(null)' with options: None
System.ArgumentException: Format of the initialization string does not conform to specification starting at index 0.
at System.Data.Common.DbConnectionOptions.GetKeyValuePair(String connectionString, Int32 currentPosition, StringBuilder buffer, Boolean useOdbcRules, String& keyname, String& keyvalue)

I already searched for a solution in the web and what I have found is that basically the problem resides in the connection string but my connection strings are correct (I have review them several times). This is the content of the ConnectionStrings section in the appsettings.json file:

{
"ConnectionStrings": {
"DefaultConnection": "host=localhost; database=elisur; schema=elisur; port=5434; user id=postgres; password=pass01; License Key=n7UBDBU7VZ2bJ2bnpFvq0WiQliEDxJRPtvByFjBXw5SwmZj1oyZg+lVDiR7b0/sTC5EZOf4dUDtrzLJj/RxA+fkRIgcDTtyc3t4oI0il+q//ek8QqTzeIicZUQo2xm0fZdfDk4mYSqxdlesin3VxZ5BR/HLsWzAdukPypvM52xrf0mbVczcWoyEfwah4lb/q/1SZnvgccHWhcIY+IWAXxutlmm1Hg/8aW5hMUUK3HF5gsHzR/YcXz54GGmiq2/E9LSiIZya+Lwv4VboCCP2mI9ZMZ23/GzHd4jGykCqdOnE=",
"ElisurConnection": "host=localhost; database=elisur; schema=elisur; port=5434; user id=postgres; password=pass01; License Key=n7UBDBU7VZ2bJ2bnpFvq0WiQliEDxJRPtvByFjBXw5SwmZj1oyZg+lVDiR7b0/sTC5EZOf4dUDtrzLJj/RxA+fkRIgcDTtyc3t4oI0il+q//ek8QqTzeIicZUQo2xm0fZdfDk4mYSqxdlesin3VxZ5BR/HLsWzAdukPypvM52xrf0mbVczcWoyEfwah4lb/q/1SZnvgccHWhcIY+IWAXxutlmm1Hg/8aW5hMUUK3HF5gsHzR/YcXz54GGmiq2/E9LSiIZya+Lwv4VboCCP2mI9ZMZ23/GzHd4jGykCqdOnE=",
"RioPoderosoConnection": "host=localhost; database=riopoderoso; schema=riopoderoso; port=5434; user id=postgres; password=pas001; License Key=n7UBDBU7VZ2bJ2bnpFvq0WiQliEDxJRPtvByFjBXw5SwmZj1oyZg+lVDiR7b0/sTC5EZOf4dUDtrzLJj/RxA+fkRIgcDTtyc3t4oI0il+q//ek8QqTzeIicZUQo2xm0fZdfDk4mYSqxdlesin3VxZ5BR/HLsWzAdukPypvM52xrf0mbVczcWoyEfwah4lb/q/1SZnvgccHWhcIY+IWAXxutlmm1Hg/8aW5hMUUK3HF5gsHzR/YcXz54GGmiq2/E9LSiIZya+Lwv4VboCCP2mI9ZMZ23/GzHd4jGykCqdOnE=",
"SQLConnection": "Server=tcp:409951.database.windows.net,1433;Initial Catalog=SDT;Persist Security Info=False;User ID=sensei;Password={your_password};MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"
}


Connection strings have a parameter called License Key which is required by the ADO.NET Data Provider I use which is dotConnect for PostgreSQL.
Database and schema already exist and the DB engine service is running in port 5434.

Also, the program.cs contains the following code. Although there exists a ApplicationDbContext, it is not used for now.

builder.Services.
AddDbContext<ApplicationDbContext>(opts1 => opts1.UsePostgreSql(builder.Configuration.GetConnectionString("DefaultConnection"))).
AddDbContext<RioPoderosoDbContext>(opts2 => opts2.UsePostgreSql(builder.Configuration.GetConnectionString("RioPoderosoConnection"))).
AddDbContext<ElisurDbContext>(opts3 => opts3.UsePostgreSql(builder.Configuration.GetConnectionString("ElisurConnection")));
builder.Services.AddIdentity<IdentityUser, IdentityRole>(
options => {
options.SignIn.RequireConfirmedAccount = true;
options.SignIn.RequireConfirmedEmail = true;
//Other options go here
}
)
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddEntityFrameworkStores<ElisurDbContext>()
.AddEntityFrameworkStores<RioPoderosoDbContext>()
.AddDefaultTokenProviders();


I will very much appreciate your feedback as I have been struggling with this issue for some days.

With respect,
Jorge Maldonado



dotnet-aspnet-core-mvc
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

It seems that the problem appears when setting the -Connection argument in update-database command.
I modified my project keeping only ApplicationDbContext, I also deleted ElisurConnection and RioPoderosoConnection and kept DefaultConnection for simplicity.
I successfuly ran the following comand:

add-migration -Name CreateIdentitySchema -OutputDir Data\Migrations\Application -Context ApplicationDbContext

Next I ran the following and the error appeared:

update-database -Connection DefaultConnection -Context ApplicationDbContex

Then I tried the following and it worked correctly.

update-database -Context ApplicationDbContext

Do not know what is going on because, if I type get-help update-database the following is displayed:

Update-Database [[-Migration] <String>] [-Connection <String>] [-Context <String>] [-Project <String>] [-StartupProject <String>] [-Args <String>] [<CommonParameters>]

As you can see, -Connection is an argument that can be specified.


0 Votes 0 ·

1 Answer

JORGEMALDONADOBARRERA-7687 avatar image
0 Votes"
JORGEMALDONADOBARRERA-7687 answered

As per my comment above, I returned my project to the original state with all the connection strings and contexts, and tried the update-database without the -Connection argument and everything worked just fine.

The solution is not to specify the -Connection argument in the update-database command but just the -Context argument. Each Identity Schema was created correctly in its corresponding database.

Regards,
Jorge Maldonado





5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.