FreeSql (ix) delete data

Delete is a very dangerous operation, FreeSql to delete support is not strong, only supports a single method to delete Table conditional.

I do not want to introduce too many series stretched to delete data, delete data introduced only this one.

If the Where condition is empty execution method, FreeSql returns only 0 or default values, do not perform the actual SQL delete operations.

To enhance the security of the system, it is strongly recommended to increase is_deledted identified in the field to make the soft-deleted entity.

var connstr = "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;" + 
    "Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=10";

IFreeSql fsql = new FreeSql.FreeSqlBuilder()
    .UseConnectionString(FreeSql.DataType.MySql, connstr)
    .UseAutoSyncStructure(true) //自动同步实体结构到数据库
    .Build();

[Table(Name = "tb_topic")]
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

Delete<Topic>(object dywhere)

dywhere support

  • Primary key
  • new [] {1 master key, the master key 2}
  • Topic object
  • new [] {Topic objects 1, Topic objects 2}
  • new { id = 1 }
fsql.Delete<Topic>(new[] { 1, 2 }).ExecuteAffrows();
//DELETE FROM `tb_topic` WHERE (`Id` = 1 OR `Id` = 2)

fsql.Delete<Topic>(new Topic { Id = 1, Title = "test" }).ExecuteAffrows();
//DELETE FROM `tb_topic` WHERE (`Id` = 1)

fsql.Delete<Topic>(new[] { new Topic { Id = 1, Title = "test" }, new Topic { Id = 2, Title = "test" } }).ExecuteAffrows();
//DELETE FROM `tb_topic` WHERE (`Id` = 1 OR `Id` = 2)

fsql.Delete<Topic>(new { id = 1 }).ExecuteAffrows();
//DELETE FROM `tb_topic` WHERE (`Id` = 1)

Delete Conditions

For security reasons, do not delete without action, to avoid accidentally deleted the entire table data
to delete the whole table data:.. Fsql.Delete <T> ( ) Where ( "1 = 1") ExecuteAffrows ()

fsql.Delete<Topic>().Where(a => a.Id == 1).ExecuteAffrows();
//DELETE FROM `tb_topic` WHERE (`Id` = 1)

fsql.Delete<Topic>().Where("id = ?id", new { id = 1 }).ExecuteAffrows();
//DELETE FROM `tb_topic` WHERE (id = ?id)

var item = new Topic { Id = 1, Title = "newtitle" };
fsql.Delete<Topic>().Where(item).ExecuteAffrows();
//DELETE FROM `tb_topic` WHERE (`Id` = 1)

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 });
fsql.Delete<Topic>().Where(items).ExecuteAffrows();
//DELETE FROM `tb_topic` WHERE (`Id` IN (1,2,3,4,5,6,7,8,9,10))

API

method return value parameter description
Where <this> Lambda Expression conditions, only the physical infrastructure to support members (not contain navigation objects)
Where <this> string, parms Sql syntax native conditions, Where ( "id =? Id", new {id = 1})
Where <this> T1 | IEnumerable Incoming entity or set as a condition to its primary key
WhereExists <this> ISelect The existence of sub-query
WithTransaction <this> DbTransaction Set transaction object
ToSql string Returns the SQL statement to be executed
ExecuteAffrows long The number of rows to execute SQL statements, the impact of return
ExecuteDeleted List<T1> Execute SQL statements, returns the deleted records

Guess you like

Origin www.cnblogs.com/FreeSql/p/11531320.html