About Connection Strings - lobodava/artisan-orm GitHub Wiki
The RepositoryBase constructor takes a connection string directly:
public RepositoryBase(string connectionString);
public RepositoryBase(SqlTransaction? transaction, string connectionString);
With only a connection string, or with a null transaction, the constructor creates an owned SqlConnection (left closed) and stores it on Connection. Individual read/execute operations open it when needed and close it afterward if they opened it.
For the source being prepared for 4.1.0, a non-null external transaction instead supplies the actual connection: Connection is that transaction's existing Connection. The constructor's string is retained as ConnectionString, but it does not redirect work to a different database or open a second connection. The caller owns both borrowed resources, and repository disposal leaves them alive. A completed/disposed transaction is rejected. See transaction ownership; check older package behavior before relying on this fix.
Earlier versions of Artisan.Orm read the connection string from
Web.config/App.configautomatically by name, with optional per-machine and per-build-configuration overrides. From version 4.0 that machinery has been removed: pick whichever .NET configuration source you like and pass the resolved connection string to the constructor.
Modern .NET — appsettings.json + DI
// appsettings.json
{
"ConnectionStrings": {
"DatabaseConnection": "Data Source=.\\SQLEXPRESS;Initial Catalog=Artisan;Integrated Security=True;TrustServerCertificate=True;"
}
}
Register repositories as Scoped so each HTTP request gets its own instance with its own connection and transaction state:
// Program.cs
var builder = WebApplication.CreateBuilder(args);
var connStr = builder.Configuration.GetConnectionString("DatabaseConnection")!;
builder.Services.AddScoped(_ => new UserRepository(connStr));
builder.Services.AddScoped(_ => new RecordRepository(connStr));
See Using Artisan.Orm in ASP.NET Core for the full lifetime / DI story (background services, scope sharing, why not Singleton / Transient).
Console / scripts / one-off code
When there is no DI container around, just using the repository:
using var repo = new UserRepository(connectionString);
var user = repo.GetUserById(1);
Connection pool
Keep connection pooling enabled for ordinary application use. With Pooling=False, opening a newly created connection establishes a new physical connection instead of borrowing one from the pool. Merely constructing a repository does not open its connection. A repository borrowing an external transaction reuses the connection that its caller already opened.
Per-environment connection strings
Use the standard ASP.NET Core configuration layering — appsettings.Development.json, appsettings.Production.json, environment variables, user secrets — instead of putting machine names into config keys.
# Pick the active environment via ASPNETCORE_ENVIRONMENT
set ASPNETCORE_ENVIRONMENT=Development & dotnet run
# Or store the developer connection string in user secrets
dotnet user-secrets set "ConnectionStrings:DatabaseConnection" "Data Source=..."
The IConfiguration.GetConnectionString("DatabaseConnection") call automatically resolves the layered value at startup.
Legacy .NET Framework
For an older Artisan.Orm package that supports .NET Framework, read App.config / Web.config explicitly and pass the connection string in. This is legacy guidance: the 4.1.0 source targets netstandard2.1, net8.0, and net10.0; it does not target .NET Framework.
var connStr = ConfigurationManager.ConnectionStrings["DatabaseConnection"].ConnectionString;
var repo = new UserRepository(connStr);
See also: