1

Facing this exception message in my GenericRepository

The ConnectionString property has not been initialized

I have already configured my connection in Startup.cs like this:

services.AddDbContext<DataContext>(opt => opt.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

I have another query where I am calling stored procedure and its fetching records. But when I am accessing DB from EF it's throwing this error.

NOTE: I have written a UNIT Test for this piece of code and it works and fetch records from DB.

Stack Trace

at System.Data.SqlClient.SqlConnection.PermissionDemand() at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource1 retry) at System.Data.SqlClient.SqlConnection.OpenAsync(CancellationToken cancellationToken) at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.d__48.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.d__45.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable1.AsyncEnumerator.<BufferlessMoveNext>d__12.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.<ExecuteAsync>d__72.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable1.AsyncEnumerator.<MoveNext>d__11.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Linq.AsyncEnumerable.SelectEnumerableAsyncIterator2.d__7.MoveNext() in D:\a\1\s\Ix.NET\Source\System.Interactive.Async\Select.cs:line 106 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult() at System.Linq.AsyncEnumerable.AsyncIterator1.d__10.MoveNext() in D:\a\1\s\Ix.NET\Source\System.Interactive.Async\AsyncIterator.cs:line 98 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.ExceptionInterceptor1.EnumeratorExceptionInterceptor.<MoveNext>d__5.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult() at System.Linq.AsyncEnumerable.d__63.MoveNext() in D:\a\1\s\Ix.NET\Source\System.Interactive.Async\Aggregate.cs:line 120 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at Repositories.GenericRepository`1.d__9.MoveNext() in Repositories\Implementations\GenericRepository.cs:line 122

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
JB's
  • 606
  • 13
  • 30
  • 1
    what's the value of Configuration.GetConnectionString("DefaultConnection") ? – gsharp Oct 24 '19 at 03:53
  • The value is `"Server=.;Database=DBName;Trusted_Connection=True;ConnectRetryCount=0;Integrated Security=SSPI;"` – JB's Oct 24 '19 at 10:10
  • @gsharp I have created a unit test to test my repository method and its working fine, giving no exceptions – JB's Oct 24 '19 at 10:12

1 Answers1

1

The problem was I was calling a SPROC before my repository, I calling SPROC in old fashion way. Got the connection string from context.Database.GetConnectionString() and using that variable in a using statement and that statement was wrapped in a try catch finally block. After my SPROC was executed I manually close the connection. Which cause the exception at the repository level. Once I closed the connection string EF would not be able to connect to the DB as its connection was disposed of in using statement.

I found 2 simple solution 1. (Recommended) I made the SPROC return multiple tables so that I don't have to call the repository again. 2. (Not Recommended) I update the Stratup.cs from services.AddScoped<DbContext, DataContext>(); to services.AddTransient<DbContext, DataContext>();. If you are not familiar with this option I higly recommend not using it. Have a look at this link for further clarification

JB's
  • 606
  • 13
  • 30