Back to site

Localdb Mssqllocaldb __exclusive__ [ Best ]

// DbContext class public class ApplicationDbContext : DbContext { public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { }

This LocalDB instance is perfect for development, testing, and lightweight applications without needing a full SQL Server installation! localdb mssqllocaldb

public static void CreateTables() { string createTablesQuery = @" CREATE TABLE Users ( Id INT PRIMARY KEY IDENTITY(1,1), Username NVARCHAR(50) NOT NULL UNIQUE, Email NVARCHAR(100) NOT NULL, CreatedAt DATETIME2 DEFAULT GETDATE() ); CREATE TABLE Products ( Id INT PRIMARY KEY IDENTITY(1,1), Name NVARCHAR(100) NOT NULL, Price DECIMAL(18,2) NOT NULL, Stock INT DEFAULT 0 );"; using (var connection = new SqlConnection(@"Server=(localdb)\MSSQLLocalDB;Database=MyApp;Trusted_Connection=true;")) { connection.Open(); using (var command = new SqlCommand(createTablesQuery, connection)) { command.ExecuteNonQuery(); Console.WriteLine("Tables created successfully!"); } } } } // appsettings.json { "ConnectionStrings": { "DefaultConnection": "Server=(localdb)\\MSSQLLocalDB;Database=MyAppDb;Trusted_Connection=true;MultipleActiveResultSets=true" } } // DbContext configuration in Program.cs (.NET 6+) using Microsoft.EntityFrameworkCore; Username NVARCHAR(50) NOT NULL UNIQUE