当实现使用小巧玲珑的ORM库模式,我目前做如下:差异
private readonly ConnectionStrings _connectionStrings;
private IDbConnection _db;
public CustomerRepository(IOptions<ConnectionStrings> connectionStrings)
{
_connectionStrings = connectionStrings.Value;
_db = new SqlConnection(_connectionStrings.DefaultConnection);
}
public Customer Find(int id)
{
return this._db.Query<Customer>("SELECT * FROM Contacts WHERE Id = @Id", new { id }).SingleOrDefault();
}
可有人请告诉我是否应该这样做,或者如果我应该在每个存储库函数中使用带有新SqlConnection的using语句。
我假设我的上面的代码需要类似UnitOfWork的东西才能生效吗?而且在完成运行所有需要的存储库功能时,也可以采用某种方式处理连接。
在每种方法中使用'using'语句。不要重用连接或任何ADO.NET对象。 [相关](http://stackoverflow.com/questions/9705637/executereader-requires-an-open-and-available-connection-the-connections-curren/9707060#9707060) –
使用'using'语句。 – stuartd
感谢球员,但你能解释为什么我看到一些例子,如何时使用EF,通过依赖注入传入上下文,然后使用?还有可能通过几个使用语句来实现UnitOfWork?我正在考虑使用以下方法:https://github.com/timschreiber/DapperUnitOfWork –