Skip to content

Migrating to MartinCostello.SqlLocalDb from System.Data.SqlLocalDb

Martin Costello edited this page Jan 18, 2022 · 4 revisions

Migrating to MartinCostello.SqlLocalDb from System.Data.SqlLocalDb

This document explains some of the steps required to migrate from System.Data.SqlLocalDb 1.x.x to MartinCostello.SqlLocalDb 2.0.0 and later. It is not exhaustive as use-cases may differ, but illustrates equivalent namespace and type names, as well as equivalents for certain tasks.

  • Remove references(s) to the System.Data.SqlLocalDb NuGet package.
  • Add reference(s) to the MartinCostello.SqlLocalDb NuGet package.
  • Change uses of the System.Data.SqlLocalDb namespace to MartinCostello.SqlLocalDb.
  • Change usage of SqlLocalDbApiWrapper for SqlLocalDbApi.
  • Remove any application settings used by System.Data.SqlLocalDb to using the SqlLocalDbOptions class in code instead (see Configuration).
  • Remove any references to the static SqlLocalDbApi class and use instance-based methods on SqlLocalDbApi/ISqlLocalDbApi and/or ISqlLocalDbInstanceManager/SqlLocalDbInstanceManager instead.
  • Change any usage of ISqlLocalDbInstance to ISqlLocalDbInstanceInfo.
  • Change any usage of static methods on the TemporarySqlLocalDbInstance class to use the CreateTemporaryInstance() extension method on the ISqlLocalDbApi interface instead.
  • For dependency injection in .NET Core applications, you can use the AddSqlLocalDB() method with IServiceCollection to register the ISqlLocalDbApi and SqlLocalDbOptions types.

How-To Guide

Check SQL Server LocalDB is installed

using var localDB = new SqlLocalDbApi();

if (!localDB.IsLocalDBInstalled())
{
    // Do something if SQL Server LocalDB is not installed
}

Get available SQL LocalDB instances

using var localDB = new SqlLocalDbApi();

var instances = localDB.GetInstances();

foreach (ISqlLocalDbInstanceInfo instanceInfo in instances)
{
    // Do something with the instances
}

Check whether an SQL LocalDB instance exists

using var localDB = new SqlLocalDbApi();

if (localDB.InstanceExists("MyInstance"))
{
    // Do something if the instance exists
}

Create a new SQL LocalDB instance

using var localDB = new SqlLocalDbApi();

ISqlLocalDbInstanceInfo instance = localDB.CreateInstance("MyInstance");
// Do something with the instance

Execute a SQL query against an instance

using var localDB = new SqlLocalDbApi();

var instance = localDB.GetInstanceInfo("MyInstance");

var manager = new SqlLocalDbInstanceManager(instance, localDB);
manager.Start();

using SqlConnection connection = manager.CreateConnection();
await connection.OpenAsync();

try
{
    // Do something with the SQL connection
}
finally
{
    await connection.CloseAsync();
}