C# 连接字符串属性尚未初始化实体框架核心
在我的GenericRepository中面对此异常消息 ConnectionString属性尚未初始化 我已经在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
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,TaskCompletionSource1重试,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.AsyncQueryingEnumerable1.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.AsyncQueryEnumerable1.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.ConfiguredTaskAwaitable1.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.ExceptionInterceptor1.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 63.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我创建了一个单元测试来测试我的存储库方法及其工作正常,没有异常