Programming/Use SQLite in Csharp: Difference between revisions
From Wiki Aghanim
Jump to navigationJump to search
Created page with "== SQLite == SQLite is a relational database that is small, and does not require much resources to run. == Use in C# == To use SQLite in your C# project you first need to install ''Sqlite'' packages from EntityFrameworkCore. <syntaxhighlight lang="bash"> dotnet add package Microsoft.EntityFrameworkCore.Sqlite </syntaxhighlight> Create a ''DBContext'' class, such as the one below. <syntaxhighlight lang="csharp"> { public class DBContext : DbContext // Custom..." |
|||
| (8 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
== SQLite == | == SQLite == | ||
SQLite is a relational database that | SQLite is a lightweight relational database that requires minimal system resources. | ||
== Use in C# == | == Use in C# == | ||
| Line 9: | Line 9: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Create a ''DBContext'' class | Create a ''DBContext'' class. | ||
<syntaxhighlight lang="csharp"> | <syntaxhighlight lang="csharp"> | ||
using Microsoft.EntityFrameworkCore; // EF Core namespace | |||
namespace SQLiteExampleTest | |||
{ | { | ||
public class DBContext : DbContext // Custom EF Core context | public class DBContext : DbContext // Custom EF Core context | ||
| Line 25: | Line 28: | ||
{ | { | ||
// Use SQLite + file path | // Use SQLite + file path | ||
optionsBuilder.UseSqlite(@"Data Source= | optionsBuilder.UseSqlite(@"Data Source=MyDdatabase.db"); | ||
} | } | ||
} | } | ||
| Line 34: | Line 37: | ||
The the protected method OnConfiguring we pass in the DBContextOptionsBuilder, and specify the name of the database we want to create. | The the protected method OnConfiguring we pass in the DBContextOptionsBuilder, and specify the name of the database we want to create. | ||
In the contructer we create the database if it is missing. This step is cruical. | In the contructer we create the database if it is missing. This step is cruical. | ||
To DI the context if you use ''ASP.NET Core'' application, add the following to your ''Program.cs''. | |||
<syntaxhighlight lang="csharp"> | |||
builder.Services.AddDbContext<BloggingContext>(options => options.UseSqlite(connectionString)) | |||
</syntaxhighlight> | |||
== Example usage == | == Example usage == | ||
| Line 48: | Line 57: | ||
[[Category:CSharp]] | [[Category:CSharp]] | ||
[[Category:Programming]] | [[Category:Programming]] | ||
Latest revision as of 23:45, 17 February 2026
SQLite
SQLite is a lightweight relational database that requires minimal system resources.
Use in C#
To use SQLite in your C# project you first need to install Sqlite packages from EntityFrameworkCore.
dotnet add package Microsoft.EntityFrameworkCore.Sqlite
Create a DBContext class.
using Microsoft.EntityFrameworkCore; // EF Core namespace
namespace SQLiteExampleTest
{
public class DBContext : DbContext // Custom EF Core context
{
public DBContext()
{
this.Database.EnsureCreated(); // Create DB if missing
}
public DbSet<PersonModel> People { get; set; } // Table: People
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
// Use SQLite + file path
optionsBuilder.UseSqlite(@"Data Source=MyDdatabase.db");
}
}
}
In the example above I have created a Models class called PeopleMode where I have the properties Name, Age and Address. The the protected method OnConfiguring we pass in the DBContextOptionsBuilder, and specify the name of the database we want to create.
In the contructer we create the database if it is missing. This step is cruical.
To DI the context if you use ASP.NET Core application, add the following to your Program.cs.
builder.Services.AddDbContext<BloggingContext>(options => options.UseSqlite(connectionString))
Example usage
var person1 = new PersonModel { Name = "Alice", Age = 30 };
var person2 = new PersonModel { Name = "Bob", Age = 25 };
context.People.Add(person1);
context.People.Add(person2);
context.SaveChanges();
Console.WriteLine("Added two persons.");