mirror of
https://github.com/dotnetcore/FreeSql.git
synced 2026-02-04 15:30:53 +08:00
update
@@ -17,13 +17,13 @@ class Topic {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var items = new List<Topic>();
|
var items = new List<Topic>();
|
||||||
for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100 });
|
for (var a = 0; a < 10; a++) items.Add(new Topic { Title = $"newtitle{a}", Clicks = a * 100 });
|
||||||
```
|
```
|
||||||
|
|
||||||
## 1. Single Insert
|
## 1. Single Insert
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
var t1 = fsql.Insert(items.First()).ExecuteAffrows();
|
var t1 = fsql.Insert(items[0]).ExecuteAffrows();
|
||||||
//INSERT INTO `Topic`(`Clicks`, `Title`, `CreateTime`)
|
//INSERT INTO `Topic`(`Clicks`, `Title`, `CreateTime`)
|
||||||
//VALUES(@Clicks0, @Title0, @CreateTime0)
|
//VALUES(@Clicks0, @Title0, @CreateTime0)
|
||||||
```
|
```
|
||||||
@@ -32,16 +32,16 @@ If the table has auto-increment columns, `id` will be returned after inserting d
|
|||||||
|
|
||||||
Method 1: (Original)
|
Method 1: (Original)
|
||||||
```csharp
|
```csharp
|
||||||
long id = fsql.Insert(blog).ExecuteIdentity();
|
long id = fsql.Insert(items[0]).ExecuteIdentity();
|
||||||
blog.Id = id;
|
items[0].Id = id;
|
||||||
```
|
```
|
||||||
|
|
||||||
Method 2: (depends on FreeSql.Repository)
|
Method 2: (depends on FreeSql.Repository)
|
||||||
```csharp
|
```csharp
|
||||||
var repo = fsql.GetRepository<Blog>();
|
var repo = fsql.GetRepository<Topic>();
|
||||||
repo.Insert(blog);
|
repo.Insert(items[0]);
|
||||||
```
|
```
|
||||||
> In the internal implementation, after inserting the data, the self-incremental value will be assigned to `blog.Id`
|
> In the internal implementation, after inserting the data, the self-incremental value will be assigned to `items[0].Id` (support batch insert backfill)
|
||||||
|
|
||||||
## 2. Batch Insert
|
## 2. Batch Insert
|
||||||
|
|
||||||
|
|||||||
25
添加.md
25
添加.md
@@ -17,31 +17,34 @@ class Topic {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var items = new List<Topic>();
|
var items = new List<Topic>();
|
||||||
for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100 });
|
for (var a = 0; a < 10; a++) items.Add(new Topic { Title = $"newtitle{a}", Clicks = a * 100 });
|
||||||
```
|
```
|
||||||
|
|
||||||
## 1、单条插入
|
## 1、单条插入
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
var t1 = fsql.Insert(items.First()).ExecuteAffrows();
|
var t1 = fsql.Insert(items[0]).ExecuteAffrows();
|
||||||
//INSERT INTO `Topic`(`Clicks`, `Title`, `CreateTime`)
|
//INSERT INTO `Topic`(`Clicks`, `Title`, `CreateTime`)
|
||||||
//VALUES(@Clicks0, @Title0, @CreateTime0)
|
//VALUES(?Clicks0, ?Title0, ?CreateTime0)
|
||||||
```
|
```
|
||||||
|
|
||||||
如果表有自增列,插入数据后应该要返回 id。
|
如果表有自增列,插入数据后应该要返回 id。
|
||||||
|
|
||||||
方法1:(原始)
|
方法 1:(原始)
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
long id = fsql.Insert(blog).ExecuteIdentity();
|
long id = fsql.Insert(items[0]).ExecuteIdentity();
|
||||||
blog.Id = id;
|
items[0].Id = id;
|
||||||
```
|
```
|
||||||
|
|
||||||
方法2:(依赖 FreeSql.Repository)
|
方法 2:(依赖 FreeSql.Repository)
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
var repo = fsql.GetRepository<Blog>();
|
var repo = fsql.GetRepository<Topic>();
|
||||||
repo.Insert(blog);
|
repo.Insert(items[0]);
|
||||||
```
|
```
|
||||||
> 内部会将插入后的自增值填充给 blog.Id
|
|
||||||
|
> 内部会将插入后的自增值填充给 items[0].Id (支持批量插入回填)
|
||||||
|
|
||||||
## 2、批量插入
|
## 2、批量插入
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user