Upgrading from 4 to 4.5 Causes Compilation Errors for DbFactory

In all my services, all my references to DbFactory

using (var db = DbFactory.OpenDbConnection())
{

are giving me ‘The name DbFactory does not exist in current context’.

The same code compiled and run fine under 4.0. I looked at the documentation and I didn’t see any breaking changes related to this.

What am I missing?

Thanks.

DbFactory was removed in v4.0.56. I usually just use Db from within a service. If you need DbFactory you’ll have to declare your own.

public virtual IDbConnectionFactory DbFactory
{
    get { return TryResolve<IDbConnectionFactory>(); }
}

I suspected that and I did go through all the release notes between 4.0.54 (the version I had) and 4.5 and didn’t see any mention of it.

I would consider this change a breaking change and it should have been mentioned explicitly.

Thanks for the help.

We obsoleted DbFactory before we removed it as all Resources on Service base class are now overridable and resolved from AppHost. This was required for Db access to be Multitenancy aware which doesn’t work when retrieving a Db connection directly from the DbFactory and therefore is recommended to use base.Db when you can.

In order to access DbFactory again you can just add a public property in your Services so it’s injected like any other dependency, e.g:

public IDbConnectionFactory DbFactory { get; set; }