简体   繁体   English

连接字符串属性尚未初始化 Entity Framework Core

[英]Connection string property has not been initialized Entity Framework Core

Facing this exception message in my GenericRepository在我的 GenericRepository 中面对这个异常消息

The ConnectionString property has not been initialized ConnectionString 属性尚未初始化

I have already configured my connection in Startup.cs like this:我已经在Startup.cs中配置了我的连接,如下所示:

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.但是当我从 EF 访问数据库时,它会抛出这个错误。

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, TaskCompletionSource 1 retry, DbConnectionOptions userOptions) at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource 1 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.Co在 System.Data.SqlClient.SqlConnection.PermissionDemand() 在 System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource 1 retry, DbConnectionOptions userOptions) at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource 1 重试) 在 System.Data.SqlClient.SqlConnection.OpenAsync(CancellationToken cancelToken) 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 在 Microsoft.EntityFrameworkCore.Storage.RelationalConnection。 d__48.MoveNext() 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 在 Microsoft.EntityFrameworkCore.Storage.RelationalConnection.d__45.MoveNext() 在 System.Runtime。 System.Runtime.Co 的 ExceptionServices.ExceptionDispatchInfo.Throw() mpilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable 1.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__7 2.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable 1.AsyncEnumerator.<MoveNext>d__11.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Linq.AsyncEnumerable.SelectEnumerableAsyncIterator mpilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 在 Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable 1.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__7 2.MoveNext() 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter。 Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable 的 HandleNonSuccessAndDebuggerNotification(任务任务) 1.AsyncEnumerator.<MoveNext>d__11.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Linq.AsyncEnumerable.SelectEnumerableAsyncIterator 1.AsyncEnumerator.<MoveNext>d__11.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Linq.AsyncEnumerable.SelectEnumerableAsyncIterator 2.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.ConfiguredTaskAwaitable 1.ConfiguredTaskAwaiter.GetResult() at System.Linq.AsyncEnumerable.AsyncIterator 1.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.ExceptionInterceptor 1.EnumeratorExceptionInterceptor.<MoveNext>d__5.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable D:\a\1\s\Ix.NET\Source\System.Interactive.Async\Select.cs 中的1.AsyncEnumerator.<MoveNext>d__11.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Linq.AsyncEnumerable.SelectEnumerableAsyncIterator 2.d__7.MoveNext():System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 处的第 106 行.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable 1.ConfiguredTaskAwaiter.GetResult() at System.Linq.AsyncEnumerable.AsyncIterator 1.d__10.MoveNext()\ in D: s\Ix.NET\Source\System.Interactive.Async\AsyncIterator.cs:第 98 行在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 在 Microsoft.EntityFrameworkCore .Query.Internal.AsyncLinqOperatorProvider.ExceptionInterceptor 1.EnumeratorExceptionInterceptor.<MoveNext>d__5.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable 1.EnumeratorExceptionInterceptor.<MoveNext>d__5.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable 1.ConfiguredTaskAwaiter.GetResult() at System.Linq.AsyncEnumerable.d__6 3.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.TaskAwaiter 1.GetResult() at Repositories.GenericRepository`1.d__9.MoveNext() in Repositories\Implementations\GenericRepository.cs:line 122 System.Runtime.CompilerServices.ConfiguredTaskAwaitable 中的 otification 1.EnumeratorExceptionInterceptor.<MoveNext>d__5.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable 1.ConfiguredTaskAwaiter.GetResult() 在 System.Linq.AsyncEnumerable.d__6 3.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.TaskAwaiter 1.GetResult()在 Repositories\Implementations\GenericRepository.cs:line 122 中的 Repositories.GenericRepository`1.d__9.MoveNext()

The problem was I was calling a SPROC before my repository, I calling SPROC in old fashion way.问题是我在我的存储库之前调用了一个 SPROC,我以老式的方式调用了 SPROC。 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.context.Database.GetConnectionString()获取连接字符串并在 using 语句中使用该变量,该语句被包装在 try catch finally 块中。 After my SPROC was executed I manually close the connection.执行 SPROC 后,我手动关闭连接。 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.一旦我关闭连接字符串 EF 将无法连接到数据库,因为它的连接已在 using 语句中处理。

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 个简单的解决方案 1。(推荐)我让 SPROC 返回多个表,这样我就不必再次调用存储库了。 2. (Not Recommended) I update the Stratup.cs from services.AddScoped<DbContext, DataContext>(); 2.(不推荐)我从services.AddScoped<DbContext, DataContext>();更新了Stratup.cs to services.AddTransient<DbContext, DataContext>();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 查看此链接以获取进一步说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM