Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 连接字符串属性尚未初始化实体框架核心_C#_.net_Entity Framework_Asp.net Core_Ef Core 2.0 - Fatal编程技术网

C# 连接字符串属性尚未初始化实体框架核心

C# 连接字符串属性尚未初始化实体框架核心,c#,.net,entity-framework,asp.net-core,ef-core-2.0,C#,.net,Entity Framework,Asp.net Core,Ef Core 2.0,在我的GenericRepository中面对此异常消息 ConnectionString属性尚未初始化 我已经在Startup.cs中配置了我的连接,如下所示: services.AddDbContext<DataContext>(opt => opt.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); services.AddDbContext(opt=>opt.UseSqlServ

在我的GenericRepository中面对此异常消息

ConnectionString属性尚未初始化

我已经在
Startup.cs
中配置了我的连接,如下所示:

services.AddDbContext<DataContext>(opt => opt.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddDbContext(opt=>opt.UseSqlServer(Configuration.GetConnectionString(“DefaultConnection”));
我还有另一个查询,我在其中调用存储过程及其获取记录。但当我从EF访问DB时,它抛出了这个错误

注意:我已经为这段代码编写了一个单元测试,它可以工作并从数据库中获取记录

堆栈跟踪

位于System.Data.SqlClient.SqlConnection.PermissionDemand()处 在System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnectionOuterConnection,DbConnectionFactory connectionFactory,TaskCompletionSource
1重试,DbConnectionOptions用户选项)
在System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource
1重试) 位于System.Data.SqlClient.SqlConnection.OpenAsync(CancellationToken CancellationToken) 在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()中 在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中 在Microsoft.EntityFrameworkCore.Storage.RelationalConnection.d_u48.MoveNext()中 在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()中 在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中 在Microsoft.EntityFrameworkCore.Storage.RelationalConnection.d_u45.MoveNext()中 在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()中 在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中 在Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable
1.AsyncEnumerator.d_u12.MoveNext()中
在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()中
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
位于Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.d_u7
2.MoveNext() 在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()中 在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中 在Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryEnumerable
1.AsyncEnumerator.d_u11.MoveNext()中
在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()中
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在d:\a\1\s\Ix.NET\Source\System.Interactive.Async\Select.cs第106行中的System.Linq.AsyncEnumerable.selectEnumerableAsyncierator中
在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()中
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在System.Runtime.CompilerServices.ConfiguredTaskAwaitable
1.ConfiguredTaskAwaiter.GetResult()中
位于d:\a\1\s\Ix.NET\Source\System.Interactive.Async\asyncierator.cs中的System.Linq.AsyncEnumerable.asyncierator
1.d\u 10.MoveNext() 在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()中 在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中 在Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.ExceptionInterceptor
1.EnumeratorExceptionInterceptor.d_u5.MoveNext()中
在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()中
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在System.Runtime.CompilerServices.ConfiguredTaskAwaitable
1.ConfiguredTaskAwaiter.GetResult()中 在d:\a\1\s\Ix.NET\Source\System.Interactive.Async\Aggregate.cs中的System.Linq.AsyncEnumerable.d\u 6
3.MoveNext()处:第120行
在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()中
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
at System.Runtime.CompilerServices.TaskAwaiter
1.GetResult() 在Repositories.GenericRepository`1.d_u9.MoveNext()中 Repositories\Implements\GenericRepository.cs:第122行


问题是我在我的存储库之前调用了一个存储过程,我用旧的方式调用存储过程。从
context.Database.GetConnectionString()
获取连接字符串,并在using语句中使用该变量,该语句被包装在try-catch-finally块中。执行存储过程后,我手动关闭连接。这会导致存储库级别出现异常。一旦我关闭了连接字符串,EF将无法连接到DB,因为它的连接在using语句中被处理掉了

我找到了两个简单的解决方案 1.(推荐)我让存储过程返回多个表,这样我就不必再次调用存储库。
2.(不推荐)我从
services.AddScoped()更新
Stratup.cs
服务。AddTransient()。如果您不熟悉这个选项,我强烈建议您不要使用它

配置的值是什么。GetConnectionString(“DefaultConnection”)?值是
“服务器=;数据库=数据库名;可信连接=True;ConnectRetryCount=0;集成安全=SSPI;”
@gsharp我创建了一个单元测试来测试我的存储库方法及其工作正常,没有异常