Un-register database from connection factory

We have a requirement to temporarily attach SQLite databases on an ad-hoc basis in order to perform Import/Export/Targeted Backups, etc.

I can attach the db and perform the operation just fine, but I can’t find a way to un-register the db when we have finished with it.

Is this possible?

Regards,
Alan

There’s no supported API for it but if it’s registered as a named connection you could access the NamedConnections dictionary directly and remove it that way. SQLite retains a persistent connection which you’ll need to unset when disposing the DB connection to dispose its internal persistent connection, e.g. something like:

if (OrmLiteConnectionFactory.NamedConnections
      .TryRemove(sqliteDb, out var sqliteFactory))
{
    using (sqliteFactory.CreateDbConnection())
    {
        sqliteFactory.AutoDisposeConnection = true;
    }
}

That worked perfectly, thank you.

1 Like