Quick Examples
Installation
The example below shows how to install the IceCoffee.Db4Net package. To install other packages, see the Packages section.
Install via the NuGet Package Manager Console
Install-Package IceCoffee.Db4Net
Or via the .NET Core command line interface
dotnet add package IceCoffee.Db4Net
Installing Database Providers
- SQL Server
Install-Package Microsoft.Data.SqlClient
- SQLite
Install-Package Microsoft.Data.SQLite
- PostgreSQL
Install-Package Npgsql
- MySQL
Install-Package MySql.Data
- DaMeng
Install-Package SqlSugarCore.Dm
Usage
1. Introducing namespaces
using IceCoffee.Db4Net;
using IceCoffee.Db4Net.OptionalAttributes;
using IceCoffee.Db4Net.Extensions;
using IceCoffee.Db4Net.Core;
2. Register database connection
string connectionString = "Data Source=InMemorySample;Mode=Memory;Cache=Shared";
Db.Register(DatabaseProvider.SQLite, connectionString);
3. Define entity
[Table("foo")]
public class Foo
{
[UniqueKey, DatabaseGenerated]
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
[Column("created_at")]
public DateTime CreatedAt { get; set; }
}
4. Insert an entity
var foo = new Foo { Name = "Tom" };
Db.Insert(foo).Execute();
The generated SQL will be:
INSERT INTO [foo] ([Name], [Age], [created_at]) VALUES (@Name, @Age, @CreatedAt)
5. Get an entity by ID
var foo = Db.Query<Foo>(1).GetSingleOrDefault();
The generated SQL will be:
SELECT [Id], [Name], [Age], [created_at] AS [CreatedAt] FROM [foo] WHERE [Id] = @p1
6. Update an entity
var foo = new Foo { Id = 0, Name = "James" };
Db.Update(foo).Execute();
The generated SQL will be:
INSERT INTO [foo] ([Name], [Age], [created_at]) VALUES (@Name, @Age, @CreatedAt)
7. Delete an entity by ID
Db.Delete<Foo>(0).Execute();
The generated SQL will be:
DELETE FROM [foo] WHERE [Id] = @p1