FreeSql (xii) the specified column update data

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; }
}

Updates the designated column

fsql.Update<Topic>(1).Set(a => a.CreateTime, DateTime.Now).ExecuteAffrows();
//UPDATE `tb_topic` SET `CreateTime` = '2018-12-08 00:04:59' WHERE (`Id` = 1)

Support Set () multiple times, the equivalent of stitching

Updates the designated column, accumulation

fsql.Update<Topic>(1).Set(a => a.Clicks + 1).ExecuteAffrows();
//UPDATE `tb_topic` SET `Clicks` = ifnull(`Clicks`,0) + 1 WHERE (`Id` = 1)

Updates the designated column, once designated

fsql.Update<Topic>(1).Set(a => new Topic {
    Clicks = a.Clicks + 1,
    Time = DateTime.Now
}).ExecuteAffrows();
//UPDATE `tb_topic` SET `Clicks` = `Clicks` + 1, Time = now() WHERE (`Id` = 1)

Custom SQL

update.SetRaw("Title = @title", new { title = "新标题" }).Where("Id = @id", 1).ExecuteAffrows();
//UPDATE `tb_topic` SET Title = @title WHERE (Id = @id)

API

method return value parameter description
SetSource <this> T1 | IEnumerable Update data, set the updated entity
Set <this> Lambda, value Setting a new value column, Set (a => a.Name, "newvalue")
Set <this> Lambda Setting a new value of the column on the basis of the increase, Set (a => a.Clicks + 1), corresponding to clicks = clicks + 1;
SetRaw <this> string, parms Set value, a custom SQL syntax, SetRaw ( "title =? Title", new {title = "newtitle"})
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
ExecuteUpdated List<T1> Execute SQL statements, returns the updated record

Guess you like

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