Skip to content

SQL Server Overview

Weasel.SqlServer provides schema management and migration support for Microsoft SQL Server databases using the Microsoft.Data.SqlClient driver.

Installation

bash
dotnet add package Weasel.SqlServer

Key Components

  • SqlServerProvider -- Singleton at SqlServerProvider.Instance that handles type mappings and identifier parsing. The default schema is dbo.
  • SqlServerMigrator -- Generates DDL scripts, executes schema migrations, and manages schema creation/deletion for SQL Server.

Supported Schema Objects

ObjectClassNamespace
TablesTableWeasel.SqlServer.Tables
Stored ProceduresStoredProcedureWeasel.SqlServer.Procedures
FunctionsFunctionWeasel.SqlServer.Functions
SequencesSequenceWeasel.SqlServer
Table TypesTableTypeWeasel.SqlServer.Tables

Connection String

Weasel.SqlServer uses standard SQL Server connection strings with Microsoft.Data.SqlClient:

cs
var connectionString = "Server=localhost;Database=mydb;User Id=sa;Password=YourPassword;TrustServerCertificate=true;";

snippet source | anchor

Creating a Migrator

cs
var migrator = new SqlServerMigrator();

snippet source | anchor

The migrator can also ensure the target database exists before running migrations:

cs
await using var conn = new SqlConnection(connectionString);
await migrator.EnsureDatabaseExistsAsync(conn);

snippet source | anchor

Schema Management

SQL Server schemas are created automatically when needed during migration. You can also generate schema DDL directly:

cs
var migrator = new SqlServerMigrator();
var writer = new StringWriter();
migrator.WriteSchemaCreationSql(new[] { "myschema" }, writer);
// Generates: IF NOT EXISTS (...) EXEC('CREATE SCHEMA [myschema]');

snippet source | anchor

Released under the MIT License.