Guidance to proper use of Umbraco.Core.Persistence.Database
I have been running into the dreaded There is already an open DataReader associated with this Command which must be closed first error, and it got me questioning how we are using the Database() class.
Here is a very generic example of how it has been used:
var connectionString = "umbracoDbDSN";
var db = new Database(connectionString);
var repo1 = new Repo1(db);
var repo2 = new Repo2(db);
repo1.SomeQuery();
repo2.SomeOtherQuery();
Basically reuse the context(Is it the correct word for it?) between repos.
Then I stumbled across this article when reading this stackoverflow post which basically says that you should be creating a new context for each query.
Which I would roughly interpret as such:
public class Repo1
{
private string _connectionString;
public class Repo1(string connectionString)
{
_connectionString = connectionString;
}
public string SomeQuery()
{
string result = null;
using(var db = new Database(_connectionString))
{
result = db.SingleOrDefault("SELECT TOP 1 CustomerName FROM Customers")
}
return result;
}
}
Now I don't know if this approach applies to the Database class since I am not familiar with how it works. So can anyone tell me which one is the proper implementation or preferably point to a third if both are incorrect?
Guidance to proper use of Umbraco.Core.Persistence.Database
I have been running into the dreaded
There is already an open DataReader associated with this Command which must be closed first
error, and it got me questioning how we are using the Database() class.Here is a very generic example of how it has been used:
Basically reuse the context(Is it the correct word for it?) between repos.
Then I stumbled across this article when reading this stackoverflow post which basically says that you should be creating a new context for each query.
Which I would roughly interpret as such:
Now I don't know if this approach applies to the
Database
class since I am not familiar with how it works. So can anyone tell me which one is the proper implementation or preferably point to a third if both are incorrect?If your using the current Umbraco database you don't need to create an instance of it, you can access it via the ApplicationContext eg.
You can then pass this instance to your repos.
Isn't that the same as passing
new Database
to the repos? I am just wondering what the difference is.is working on a reply...