Recently, I upgraded my project from .net core3 to .net6, as part of this upgrade I had to upgrade Entity framework core package from 2.1.4 to 6.0.4, I also changed the namespace System.data.SqlClient to Microosft.Data.SqlClient. After these changes, I am seeing runtime exceptions in my project.
Exception detail:
System.InvalidOperationException at Aclara.CE.Notification.Delivery.Data.NotificationDeliveryRepository+<GetNotificationDeliveryByRequestIdClientId>d__24.MoveNext
OuterMessage:
The ConnectionString property has not been initialized.
outerMethod:
Microsoft.Data.SqlClient.SqlConnection.PermissionDemand
Code:
public async Task<Common.Entities.Delivery> GetNotificationDeliveryByRequestIdClientId(string notificationRequestId,
int clientId)
{
Common.Entities.Delivery deliveryContext = null;
try
{
deliveryContext = await (from delivery in _context.Delivery
where delivery.NotificationRequestId == notificationRequestId
where delivery.ClientId == clientId
select delivery).FirstOrDefaultAsync();
}
catch (Exception ex)
{
Logger.LogCritical(ex, $"{DeliveryComponent}.{nameof(GetNotificationDeliveryByRequestIdClientId)}.Exception");
}
return deliveryContext;
}
Context Class
public NotificationDeliveryRepository(NotificationsDeliveryContext deliveryContext)
{
_context = deliveryContext;
}
public NotificationDeliveryRepository(string deliverySqlConnStr, ILogger logger)
{
var options = new DbContextOptionsBuilder<NotificationsDeliveryContext>().UseSqlServer(deliverySqlConnStr)
.Options;
_context = new NotificationsDeliveryContext(options);
Logger = logger ?? throw new ArgumentException(nameof(logger));
}
Startup.cs
class Startup : FunctionsStartup {
public override void Configure(IFunctionsHostBuilder builder)
{
var configBuilder = new ConfigurationBuilder()
// .SetBasePath(@"C:\\Aclara\\CommonComponents\\AppLib\\Aclara.CE.Notification.Delivery")
.SetBasePath(Environment.CurrentDirectory)
.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
var settings = GetSettings(configBuilder);
var config = configBuilder;
var deliverySqlConnStr = config.GetConnectionString("DeliverySqlConnectionString") ??
throw new Exception("DeliverySqlConnectionString configuration is missing");
var notificationDeliveryFactory = new NotificationDeliveryFactory();
var profile = new NotificationDeliveryMappingConfigProfile();
var configProvider = new MapperConfiguration(cfg => cfg.AddProfile(profile));
builder.Services.AddDbContextPool<NotificationsDeliveryContext>(options => options.UseSqlServer(deliverySqlConnStr));
builder.Services.AddScoped<INotificationDeliveryRepository>(s => new NotificationDeliveryRepository(s.GetRequiredService<NotificationsDeliveryContext>()));
builder.Services.AddSingleton<IConfiguration>(configBuilder);
builder.Services.AddSingleton(settings);
builder.Services.AddSingleton(configProvider);
builder.Services.AddSingleton(notificationDeliveryFactory);
}