- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有时会引发此异常:
System.InvalidOperationException: 'The ConnectionString property has not been initialized.'
我使用内置的依赖注入(inject):
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<IDbConnection>(db => new SqlConnection(
Configuration.GetConnectionString("AppConnectionString")));
services.AddScoped<IAppConfigurationRepository, AppConfigurationRepository>();
services.AddScoped<IHomeworkingRequestRepository, HomeworkingRequestRepository>();
services.AddScoped<IEmployeeRepository, EmployeeRepository>();
services.AddScoped<IEmployeeService, EmployeeService>();
services.AddScoped<IHomeworkingRequestService, HomeworkingRequestService>();
services.AddMvc();
}
之前,我已经有这个错误了。代码services.AddScoped<IDbConnection>
我改为services.AddTransient<IDbConnection>
并解决了这个问题。但是现在我又遇到了错误。
编辑
请查找异常发生时的代码:
public class EmployeeRepository : IEmployeeRepository
{
private readonly IDbConnection _connection;
public EmployeeRepository(IDbConnection connection)
{
_connection = connection;
}
public IEnumerable<Employee> GetAllActiveEmployees()
{
string query = @"
SELECT
FirstName
,LastName
,BusinessUnit
FROM Employees";
using (var db = _connection)
{
_connection.Open(); // <-- The exception is thrown here
return db.Query<Employee>(query);
}
}
}
还请找到完整的堆栈跟踪:
at System.Data.SqlClient.SqlConnection.PermissionDemand()
at System.Data.SqlClient.SqlConnectionFactory.PermissionDemand(DbConnection outerConnection)
at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.Open()
at Homeworking.Dal.EmployeeRepository.GetAllActiveEmployees() in C:\Users\florian.nouri\source\repos\Homeworking\Homeworking.Repository\EmployeeRepository.cs:line 42
at Homeworking.Service.EmployeeService.GetAllEmployees() in C:\Users\florian.nouri\source\repos\Homeworking\Homeworking.Service\EmployeeService.cs:line 22
at Homeworking.Service.HomeworkingRequestService.GetAllEmployees() in C:\Users\florian.nouri\source\repos\Homeworking\Homeworking.Service\HomeworkingRequestService.cs:line 23
at Homeworking.Web.Controllers.AppController.Index() in C:\Users\florian.nouri\source\repos\Homeworking\Homeworking.Web\Controllers\AppController.cs:line 22
at lambda_method(Closure , Object , Object[] )
at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync()
最佳答案
using (var db = _connection)
这很糟糕。您为同一连接创建了一个新变量,因此在使用 block 完成后,您的连接将被释放。你不应该这样做。如果你使用容器来创建东西,容器应该处理那些它不再需要的实例。
最有可能发生的情况是您的 _connection
变量被处理掉,并且任何从容器获取连接的类(在 Scoped 的情况下)或任何已经拥有此变量并使用它的类第二次因为它是一个实例字段,将使用已经处理好的连接。
关于c# - 依赖注入(inject) IDbConnection SqlConnection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50366502/
使用 DataSnap 2009,我似乎可以在客户端数据模块中使用远程数据库连接,使用 TDSProviderConection 和关联的 TSQLConnection。客户端很容易设置,我在表单上放
我很好奇为什么会这样。我今天早些时候遇到了这种情况 using (SqlConnection oConn = new SqlConnection(ConnectionString)) { us
我正在用 .Net Reflector 检查 SqlConnection . 我注意到有一个私有(private)构造函数,有趣的是它接受一个 SqlConnection 类作为参数。 我想知道的主要
我的问题:本周早些时候,我接到了加速我们程序中的任务的任务。我看着它,立即想到了在该任务中为函数使用并行 foreach 循环的想法。 我实现了它,通过了函数(包括所有子函数)并更改了 SqlConn
我想执行一个 SELECT 查询,然后执行一系列 UPDATE 查询(都在同一个表上); UPDATE 在重复调用的单独方法中实现。如果其中一个 UPDATE 查询失败,我希望它们全部失败/回滚 -
做什么 using (SqlConnection cn = new SqlConnection(connectionString)) 做吗? 最佳答案 new SqlConnection(connec
您好,我想问一下,创建 Singleton 以仅与 db 建立一个事件连接是否是个好主意。我想做的是: 1) 我有 wcf 服务 2)wcf服务从db获取数据 3)我想创建一个这样的单例,只有一个连接
尊敬的专业人士请帮我解决以下问题。在我的 .NET C# 应用程序中,有以下代码: SqlConnection connection = new SqlConnection( SQLCONNECTIO
我正在尝试更新一些代码。我有一个以此开头的 vb 文件... Imports System.Data.SqlClient Imports System.Data.Sql Imports System.
如果我打开一个到 SQL Server 的 SqlConnection,然后从多个后台线程发出多个查询,所有查询都使用该一个连接 - 这些查询是否会按顺序执行(不关心顺序) )? 具体来说,如果在一个
我们有一个使用 SQL Server 2008 R2 数据库的应用程序。在应用程序中,使用 SqlConnection 对象调用数据库。 此SqlConnection 对象在第一次访问时初始化一次,然
我正在尝试优化访问 SQL Server 2014 数据库的代码性能,并注意到连接池似乎并不像 ADO.NET 所宣传的那样工作。默认情况下,它应该被启用并且开箱即用(只要使用相同的连接字符串)。然而
我正在尝试使用 SqlConnection 和 SqlCommand 对象建立一个简单的 MS Access 数据库连接。 正如您在此处看到的,我是如何建立联系的: private SqlConnec
我有一个连接到数据库的 .NET 应用程序。 在我的 web.config 文件中有 用户名和密码已删除。 在我后面的代码中,我这样打开连接 protected void grabDat
SqlDataReader rdr = null; con = new SqlConnection(objUtilityDAL.ConnectionString); using (SqlCommand
public class db { public static string connectionString = WebConfigurationManager.Con
我需要你的帮助(眼睛)看看我哪里错了。 所以,我遇到了这个错误... 这是我正在做的... 一个登录用户,有一个角色,这个角色有或没有一些权限。 当他输入表单时,应用程序会根据他的角色检查用户权限。在
我不确定嵌套的 SqlConnections 是否可行,但我真的很想远离它。我现在在我的代码中遇到了一个范围问题,我正试图弄清楚如何解决它。 所以直到最近我还有一个全局 SqlConnection,它
我编写了以下代码(为简洁起见进行了删减): using (SqlConnection cn = new SqlConnection("Server=test;Database=test;User=te
我在另一台服务器上读取和写入数据到 Sql Server,因此我需要打开许多 SqlConnection,我在读取或写入完成后关闭它们,但有时我无法连接到服务器但是在我重新启动服务器后一切正常,我猜这
我是一名优秀的程序员,十分优秀!