You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When attempting to update the database using Entity Framework Core, the following exception occurs:
Formatof the initialization stringdoes not conform to specification starting at index 0.
To reproduce
I have created a 3-tier architecture:
Tier 1: Razor Pages project
Tier 2: Domain and ApplicationDbContext
Tier 3: Repository layer
2.Below is the code from the Razor Pages Program.cs file:
usingDigitalSociety.Data;usingMicrosoft.EntityFrameworkCore;varbuilder=WebApplication.CreateBuilder(args);// Add services to the container.builder.Services.AddRazorPages();builder.Services.AddDbContext<ApplicationDbContext>(options =>{options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"));});varapp=builder.Build();// Configure the HTTP request pipeline.if(!app.Environment.IsDevelopment()){app.UseExceptionHandler("/Error");// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.app.UseHsts();}app.UseHttpsRedirection();app.UseStaticFiles();app.UseRouting();app.UseAuthorization();app.MapRazorPages();app.Run();
Below is the project reference for the Razor Pages UI project:
System.ArgumentException: Format of the initialization string does not conform to specification starting at index 0.
at Microsoft.Data.Common.DbConnectionOptions.GetKeyValuePair(String connectionString, Int32 currentPosition, StringBuilder buffer, Boolean useOdbcRules, String& keyname, String& keyvalue) in /_/src/Microsoft.Data.SqlClient/src/Microsoft/Data/Common/DbConnectionOptions.Common.cs:line 475
at Microsoft.Data.Common.DbConnectionOptions.ParseInternal(Dictionary`2 parsetable, String connectionString, Boolean buildChain, Dictionary`2 synonyms, Boolean firstKey) in /_/src/Microsoft.Data.SqlClient/src/Microsoft/Data/Common/DbConnectionOptions.Common.cs:line 675
at Microsoft.Data.Common.DbConnectionOptions..ctor(String connectionString, Dictionary`2 synonyms) in /_/src/Microsoft.Data.SqlClient/src/Microsoft/Data/Common/DbConnectionOptions.Common.cs:line 121
at Microsoft.Data.SqlClient.SqlConnectionString..ctor(String connectionString) in /_/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlConnectionString.cs:line 297
at Microsoft.Data.SqlClient.SqlConnectionFactory.CreateConnectionOptions(String connectionString, DbConnectionOptions previous) in /_/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlConnectionFactory.cs:line 142
at Microsoft.Data.ProviderBase.DbConnectionFactory.GetConnectionPoolGroup(DbConnectionPoolKey key, DbConnectionPoolGroupOptions poolOptions, DbConnectionOptions& userConnectionOptions) in /_/src/Microsoft.Data.SqlClient/netcore/src/Common/src/Microsoft/Data/ProviderBase/DbConnectionFactory.cs:line 216
at Microsoft.Data.SqlClient.SqlConnection.ConnectionString_Set(DbConnectionPoolKey key) in /_/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlConnectionHelper.cs:line 72
at Microsoft.Data.SqlClient.SqlConnection.set_ConnectionString(String value) in /_/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlConnection.cs:line 626
at Microsoft.Data.SqlClient.SqlConnection..ctor(String connectionString) in /_/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlConnection.cs:line 159
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerConnection.CreateDbConnection()
at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.get_DbConnection()
at Microsoft.EntityFrameworkCore.Diagnostics.RelationalLoggerExtensions.MigrateUsingConnection(IDiagnosticsLogger`1 diagnostics, IMigrator migrator, IRelationalConnection connection)
at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String connectionString, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, String connectionString, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_0.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Addition Details
Below code is my ApplicationDbContext which is in Domain Projects
using DigitalSociety.Data.Domain;
using Microsoft.EntityFrameworkCore;
namespace DigitalSociety.Data
{
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}
public DbSet<House> Houses { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
}
and also i created ApplicationDbContextFactory
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore;
namespace DigitalSociety.Data
{
public class ApplicationDbContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext>
{
public ApplicationDbContext CreateDbContext(string[] args)
{
var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
optionsBuilder.UseSqlServer("DefaultConnection");
return new ApplicationDbContext(optionsBuilder.Options);
}
}
}
Environment Details
Target Framework: .NET 8.0
SQL Server Version: SQL Server 2019
Operating System: Windows 11
The text was updated successfully, but these errors were encountered:
Think your problem is here optionsBuilder.UseSqlServer("DefaultConnection"); in the ApplicationDbContextFactory
Can see through the stack that there is the Microsoft.EntityFrameworkCore.Design calls as part of it so it is going through the design time code.
So from the above mentioned line, you are just giving EF Core a constant string with value "DefaultConnection". This is obviously not a normal connection string so it fails.
In your program.cs you do it a bit differently and actually look up the value options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"));
Any thoughts on doing the same thing in design time?
Describe the bug
When attempting to update the database using Entity Framework Core, the following exception occurs:
To reproduce
Tier 1: Razor Pages project
Tier 2: Domain and ApplicationDbContext
Tier 3: Repository layer
2.Below is the code from the Razor Pages Program.cs file:
Stack Trace
Addition Details
Below code is my ApplicationDbContext which is in Domain Projects
and also i created ApplicationDbContextFactory
Environment Details
Target Framework: .NET 8.0
SQL Server Version: SQL Server 2019
Operating System: Windows 11
The text was updated successfully, but these errors were encountered: