System.InvalidOperationException: The ConnectionString property has not been initialized [EF Core 7, Asp.Net 7]


Question: How do you resolve the error that says The Connection property has not been initialized in Asp.Net Core 7 and Entity Framework Core 7?


Login to See the Rest of the Answer

Answer: This error means that however you are defining the connection string into the Database Context Factory is somehow not initializing. This happens when you use the really new Microsoft.Extensions.Configuration.Abstruction library. The other way you could resolve this issue is by passing in the Connection String Name that is located under the ConnectionStrings section of the appsettings.json file. This way the Service Repository class where the actual declaration of Database Context is being initialized can pass in the name of the Connection String into the Configuration like below:

 public MyDbContext CreateDbContext(string nameOfConnectionString)
 {
            var builder = new DbContextOptionsBuilder<MyDbContext>();
            try
            {

                builder.UseSqlServer(
                       Configuration.GetConnectionString(nameOfConnectionString), sqlServerOptionsAction: sqlOptions =>
                       {
                           sqlOptions.EnableRetryOnFailure();

                       }).EnableDetailedErrors().EnableSensitiveDataLogging();
            }
            catch (Exception ex)
            {
                
            }


            return new MyDbContext(builder.Options);
  }



Entity Framework
published
v.0.01





For peering opportunity Autonomouse System Number: AS401345 Custom Software Development at ErnesTech Email AddressContact: [email protected]