diff --git a/src/Sample/ConsoleDb/ConsoleDb.csproj b/src/Sample/ConsoleDb/ConsoleDb.csproj new file mode 100644 index 0000000..01160c0 --- /dev/null +++ b/src/Sample/ConsoleDb/ConsoleDb.csproj @@ -0,0 +1,24 @@ + + + + Exe + net8.0 + enable + enable + https://api.nuget.org/v3/index.json;$(PackageOutputPath) + true + + + + + + + + + + + + + + + diff --git a/src/Sample/ConsoleDb/Program.cs b/src/Sample/ConsoleDb/Program.cs new file mode 100644 index 0000000..a50b83e --- /dev/null +++ b/src/Sample/ConsoleDb/Program.cs @@ -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("SELECT * FROM Products WHERE Id = @Id", new { Id = productId }); +var product3 = connection.QueryFirst("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; + +public record Product(ProductId Id, string Name); \ No newline at end of file diff --git a/src/Sample/ConsoleDb/dapper.db b/src/Sample/ConsoleDb/dapper.db new file mode 100644 index 0000000..b0d7cd6 Binary files /dev/null and b/src/Sample/ConsoleDb/dapper.db differ diff --git a/src/Sample/Sample.sln b/src/Sample/Sample.sln index 943a9df..261b53c 100644 --- a/src/Sample/Sample.sln +++ b/src/Sample/Sample.sln @@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MinimalApi", "MinimalApi\Mi EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MvcWebApp", "MvcWebApp\MvcWebApp.csproj", "{98DCCA61-3848-967E-E422-1DEDB4CB8569}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleDb", "ConsoleDb\ConsoleDb.csproj", "{A66CF326-8BE8-2BD1-25F7-0D7960DFAFC6}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -27,6 +29,10 @@ Global {98DCCA61-3848-967E-E422-1DEDB4CB8569}.Debug|Any CPU.Build.0 = Debug|Any CPU {98DCCA61-3848-967E-E422-1DEDB4CB8569}.Release|Any CPU.ActiveCfg = Release|Any CPU {98DCCA61-3848-967E-E422-1DEDB4CB8569}.Release|Any CPU.Build.0 = Release|Any CPU + {A66CF326-8BE8-2BD1-25F7-0D7960DFAFC6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A66CF326-8BE8-2BD1-25F7-0D7960DFAFC6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A66CF326-8BE8-2BD1-25F7-0D7960DFAFC6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A66CF326-8BE8-2BD1-25F7-0D7960DFAFC6}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE