From fbcbf224e8f78e430cbd01d2a815d8af821097cc Mon Sep 17 00:00:00 2001 From: AlexLEWIS Date: Fri, 6 Aug 2021 11:39:52 +0800 Subject: [PATCH] Created Delete Data (markdown) --- Delete-Data.md | 111 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 Delete-Data.md diff --git a/Delete-Data.md b/Delete-Data.md new file mode 100644 index 0000000..f475f18 --- /dev/null +++ b/Delete-Data.md @@ -0,0 +1,111 @@ +[中文](%e5%88%a0%e9%99%a4) | **English** + +Deleting data is a very dangerous operation. FreeSql does not support deletion very powerfully. By default, it only supports single-table and conditional deletion methods. + +If it is executed when the `Where` condition is empty, only `0` or the `default` value is returned, and no real SQL delete operation is performed. + +```csharp +static IFreeSql fsql = new FreeSql.FreeSqlBuilder() + .UseConnectionString(FreeSql.DataType.MySql, connectionString) + .UseAutoSyncStructure(true) //Automatically synchronize the entity structure to the database + .Build(); //Be sure to define as singleton mode + +class Topic { + [Column(IsIdentity = true, IsPrimary = true)] + public int Id { get; set; } + public int Clicks { get; set; } + public string Title { get; set; } + public DateTime CreateTime { get; set; } +} +``` + +## Dynamic Conditions +```csharp +fsql.Delete(object dywhere) +``` +`dywhere` supports: + +* Primary key value +* `new[] { 主键值1, 主键值2 }` +* Topic Object +* `new[] { TopicObject1, TopicObject2 }` +* `new { id = 1 }` + +```csharp +var t1 = fsql.Delete(new[] { 1, 2 }).ToSql(); +//DELETE FROM `Topic` WHERE (`Id` = 1 OR `Id` = 2) + +var t2 = fsql.Delete(new Topic { Id = 1, Title = "test" }).ToSql(); +//DELETE FROM `Topic` WHERE (`Id` = 1) + +var t3 = fsql.Delete(new[] { new Topic { Id = 1, Title = "test" }, new Topic { Id = 2, Title = "test" } }).ToSql(); +//DELETE FROM `Topic` WHERE (`Id` = 1 OR `Id` = 2) + +var t4 = fsql.Delete(new { id = 1 }).ToSql(); +//DELETE FROM `Topic` WHERE (`Id` = 1) +``` + +## Delete Conditions + +> For safety reasons, when there are no conditions, the delete action will not be executed to avoid deleting the entire table data by mistake. Delete the entire table data: `fsql.Delete().Where("1=1").ExecuteAffrows()` + +```csharp +var t5 = fsql.Delete().Where(a => a.Id == 1).ToSql(); +//DELETE FROM `Topic` WHERE (`Id` = 1) + +var t6 = fsql.Delete().Where("id = @id", new { id = 1 }).ToSql(); +//DELETE FROM `Topic` WHERE (id = @id) + +var item = new Topic { Id = 1, Title = "newtitle" }; +var t7 = fsql.Delete().Where(item).ToSql(); +//DELETE FROM `Topic` WHERE (`Id` = 1) + +var items = new List(); +for (var a = 0; a < 10; a++) items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100 }); +var t8 = fsql.Delete().Where(items).ToSql(); +//DELETE FROM `Topic` WHERE (`Id` IN (1,2,3,4,5,6,7,8,9,10)) +``` + +## `ISelect.ToDelete` Advanced Delete + +`IDelete` does not support navigation objects, multi-table association, etc. By default, `ISelect.ToDelete` can convert the query to `IDelete` so that the navigation object can be used to delete data: + +```csharp +fsql.Select().Where(a => a.Options.xxx == 1).ToDelete().ExecuteAffrows(); +``` +Note: This method is not to query the data to the memory and delete it cyclically. The above code generates and executes the following SQL: + +```sql +DELETE FROM `T1` WHERE id in (select a.id from T1 a left join Options b on b.t1id = a.id where b.xxx = 1) +``` + +The benefits of using this method for complex deletion: + +- Preview data before deleting to prevent mistaken deletion operations; +- Support complex deletion operations, for example: Use `Limit(10)` on `ISelect` to delete the first 10 records that meet the conditions; + + +## Reference + +- [《Database Transaction》](Database-Transaction) +- [《FreeSql 101, Part 1: Insert Data》](Insert-Data) +- [《FreeSql 101, Part 3: Update Data》](Update-Data) +- [《FreeSql 101, Part 4: Query Data》](Query-Data) +- [《Repository Layer》](Repository-Layer) +- [《Tenant》](Tenant) + +## API + +| Methods | Return | Parameters | Description | +| - | - | - | - | +| Where | \ | Lambda | Expression conditions, only support entity basic members (not including navigation objects) | +| Where | \ | string, parms | Raw SQL syntax conditions, `Where("id = @id", new { id = 1 })` | +| Where | \ | T1 \| IEnumerable\ | Pass in the entity or collection, and use its primary key as the condition | +| WhereExists | \ | ISelect | 子查询是否存在 | +| CommandTimeout | \ | int | Command timeout setting (seconds) | +| WithTransaction | \ | DbTransaction | Set transaction object | +| WithConnection | \ | DbConnection | Set the connection object | +| ToSql | string | | Returns the SQL statement to be executed. | +| ExecuteAffrows | long | | Execute SQL and return the number of rows affected. | +| ExecuteDeleted | List\ | | Execute SQL and return the deleted records. | +