-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add simple dapper+sqlite console app demo
- Loading branch information
Showing
4 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<RestoreSources>https://api.nuget.org/v3/index.json;$(PackageOutputPath)</RestoreSources> | ||
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" Version="2.1.10" /> | ||
<PackageReference Include="StructId" Version="42.*" /> | ||
<PackageReference Include="Microsoft.Data.Sqlite.Core" Version="8.0.11" /> | ||
<PackageReference Include="Ulid" Version="1.3.4" /> | ||
<PackageReference Include="Dapper" Version="2.1.35" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Update="dapper.db" CopyToOutputDirectory="Always" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using ConsoleDb; | ||
using Dapper; | ||
using Microsoft.Data.Sqlite; | ||
|
||
SQLitePCL.Batteries.Init(); | ||
|
||
using var connection = new SqliteConnection("Data Source=dapper.db") | ||
.UseStructId(); | ||
|
||
connection.Open(); | ||
|
||
// Seed data | ||
var productId = Ulid.NewUlid(); | ||
var product = new Product(new ProductId(productId), "Product"); | ||
|
||
connection.Execute("INSERT INTO Products (Id, Name) VALUES (@Id, @Name)", new Product(ProductId.New(), "Product1")); | ||
connection.Execute("INSERT INTO Products (Id, Name) VALUES (@Id, @Name)", product); | ||
connection.Execute("INSERT INTO Products (Id, Name) VALUES (@Id, @Name)", new Product(ProductId.New(), "Product2")); | ||
|
||
// showcase we can query by the underlying ulid | ||
var product2 = connection.QueryFirst<Product>("SELECT * FROM Products WHERE Id = @Id", new { Id = productId }); | ||
var product3 = connection.QueryFirst<Product>("SELECT * FROM Products WHERE Id = @Id", new { Id = new ProductId(productId) }); | ||
|
||
Console.WriteLine("Found saved product by value: " + product.Equals(product2)); | ||
Console.WriteLine("Found saved product by id: " + product.Equals(product3)); | ||
|
||
public readonly partial record struct ProductId : IStructId<Ulid>; | ||
|
||
public record Product(ProductId Id, string Name); |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters