Path: redb/redb.MSSql/Extensions/MsSqlOptionsExtensions.cs
Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.MSSql/Extensions/MsSqlOptionsExtensions.cs
----
#pragma warning disable CS0618 // implements or uses the deprecated object permissions and roles (RedbDeprecation)
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using redb.Core;
using redb.Core.Data;
using redb.Core.Extensions;
using redb.Core.Models.Configuration;
using redb.Core.Models.Contracts;
using redb.Core.Models.Security;
using redb.Core.Providers;
using redb.Core.Query;
using redb.Core.Serialization;
using redb.Core.Configuration;
using redb.Core.Services;
using redb.MSSql.Data;
using redb.MSSql.Providers;
using redb.MSSql.Sql;
namespace redb.MSSql.Extensions;
///
/// MSSQL provider extensions for RedbOptionsBuilder.
///
public static class MsSqlOptionsExtensions
{
///
/// Configures REDB to use Microsoft SQL Server as the database provider.
///
/// The options builder.
/// MSSQL connection string. If null, must be set in Configure().
/// The options builder for chaining.
///
/// services.AddRedb(options => options
/// .UseMsSql("Server=localhost;Database=redb;Trusted_Connection=true")
/// .Configure(c => c.EnablePropsCache = true));
///
public static RedbOptionsBuilder UseMsSql(
this RedbOptionsBuilder builder,
string? connectionString = null)
{
var infrastructure = (IRedbOptionsBuilderInfrastructure)builder;
if (!string.IsNullOrEmpty(connectionString))
infrastructure.Configuration.ConnectionString = connectionString;
infrastructure.SetProviderRegistration((services, config, isPro) =>
{
if (isPro)
throw new InvalidOperationException(
"Pro version requires redb.MSSql.Pro package. Use AddRedbPro() from redb.MSSql.Pro.Extensions.");
RegisterOpenSourceServices(services, config);
});
return builder;
}
///
/// Registers OpenSource MSSQL services.
/// Called by UseMsSql() and can be extended by Pro version.
///
internal static void RegisterOpenSourceServices(IServiceCollection services, RedbServiceConfiguration config)
{
// Context
if (!string.IsNullOrEmpty(config.ConnectionString))
{
services.AddScoped(_ =>
new SqlRedbContext(config.ConnectionString, config.EnableLazyReferences) { BulkInsertTimeout = config.BulkInsertTimeout });
}
// Security
services.AddScoped(_ =>
AmbientSecurityContext.GetOrCreateDefault());
// SQL Dialect
services.AddSingleton();
// Core providers
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
// Query providers
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped(sp => sp.GetRequiredService().ListProvider); // one per scope: the service's own
services.AddScoped();
// Background deletion service (singleton + hosted service)
services.AddSingleton();
services.AddSingleton(sp => sp.GetRequiredService());
services.AddHostedService(sp => sp.GetRequiredService());
// Main service
services.AddScoped();
}
}