Skip to content

Commit 7d811c4

Browse files
authored
dapper.rainbow guide (#2043)
1 parent 4f8e92f commit 7d811c4

File tree

2 files changed

+119
-1
lines changed

2 files changed

+119
-1
lines changed

Dapper.Rainbow/readme.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# Using Dapper.Rainbow in C# for CRUD Operations
2+
3+
This guide outlines how to use `Dapper.Rainbow` in C# for CRUD operations.
4+
5+
## 1. Setting Up
6+
7+
Add Dapper and Dapper.Rainbow to your project via NuGet:
8+
9+
```powershell
10+
Install-Package Dapper -Version x.x.x
11+
Install-Package Dapper.Rainbow -Version x.x.x
12+
```
13+
14+
*Replace `x.x.x` with the latest version numbers.*
15+
16+
## 2. Database Setup and Requirements
17+
18+
For `Dapper.Rainbow` to function correctly, ensure each table has a primary key column named `Id`.
19+
20+
Example `Users` table schema:
21+
22+
```sql
23+
CREATE TABLE Users (
24+
Id INT IDENTITY(1,1) PRIMARY KEY,
25+
Name VARCHAR(100),
26+
Email VARCHAR(100)
27+
);
28+
```
29+
30+
## 3. Establishing Database Connection
31+
32+
Open a connection to your database:
33+
34+
```csharp
35+
using System.Data.SqlClient;
36+
37+
var connectionString = "your_connection_string_here";
38+
using var connection = new SqlConnection(connectionString);
39+
connection.Open(); // Open the connection
40+
```
41+
42+
## 4. Defining Your Database Context
43+
44+
Define a class for your database context:
45+
46+
```csharp
47+
using Dapper;
48+
using System.Data;
49+
50+
public class MyDatabase : Database<MyDatabase>
51+
{
52+
public Table<User> Users { get; set; }
53+
}
54+
55+
public class User
56+
{
57+
public int Id { get; set; }
58+
public string Name { get; set; }
59+
public string Email { get; set; }
60+
}
61+
```
62+
63+
## 5. Performing CRUD Operations
64+
65+
### Insert
66+
67+
```csharp
68+
var db = new MyDatabase { Connection = connection };
69+
var newUser = new User { Name = "John Doe", Email = "[email protected]" };
70+
var insertedUser = db.Users.Insert(newUser);
71+
```
72+
73+
### Select
74+
75+
Fetch users by ID or all users:
76+
77+
```csharp
78+
var user = db.Users.Get(id); // Single user by ID
79+
var users = connection.Query<User>("SELECT * FROM Users"); // All users
80+
```
81+
82+
### Update
83+
84+
```csharp
85+
var userToUpdate = db.Users.Get(id);
86+
userToUpdate.Email = "[email protected]";
87+
db.Users.Update(userToUpdate);
88+
```
89+
90+
### Delete
91+
92+
```csharp
93+
db.Users.Delete(id);
94+
```
95+
96+
## 6. Working with Foreign Keys
97+
98+
Example schema for a `Posts` table with a foreign key to `Users`:
99+
100+
```sql
101+
CREATE TABLE Posts (
102+
Id INT IDENTITY(1,1) PRIMARY KEY,
103+
UserId INT,
104+
Content VARCHAR(255),
105+
FOREIGN KEY (UserId) REFERENCES Users(Id)
106+
);
107+
```
108+
109+
Inserting a parent (`User`) and a child (`Post`) row:
110+
111+
```csharp
112+
var newUser = new User { Name = "Jane Doe", Email = "[email protected]" };
113+
var userId = db.Users.Insert(newUser);
114+
115+
var newPost = new Post { UserId = userId, Content = "Hello, World!" };
116+
db.Connection.Insert(newPost); // Using Dapper for the child table
117+
```
118+

Readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Package Purposes:
2828
* Dapper.EntityFramework.StrongName
2929
* Extension handlers for EntityFramework
3030
* Dapper.Rainbow
31-
* Micro-ORM implemented on Dapper, provides CRUD helpers
31+
* Micro-ORM implemented on Dapper, provides CRUD helpers ([readme](Dapper.Rainbow\readme.md))
3232
* Dapper.SqlBuilder
3333
* Component for building SQL queries dynamically and composably
3434

0 commit comments

Comments
 (0)