首先分享一波 freesql的官方操作指南地址:https://freesql.net/guide/
里面包括了增删改查的所有操作的地方,截个图以示尊重,respect!!!
增:
IFreeSql fsql = new FreeSql.FreeSqlBuilder() .UseConnectionString(FreeSql.DataType.MySql, connectionString) .UseAutoSyncStructure(true).Build(); //请务必定义成 Singleton 单例模式
class Topic {
[Column(IsIdentity = true, IsPrimary = true)] //id自增的属性
public int Id { get; set; }
public int Clicks { get; set; }
public string Title { get; set; }
public DateTime CreateTime { get; set; }
}
var items = new List();
for (var a = 0; a < 10; a++) items.Add(new Topic { Title = $“newtitle{a}”, Clicks = a * 100 });
删:举个例子
fsql.Delete()
.Where(b => b.Url == “http://sample.com”)
.ExecuteAffrows();
改:
fsql.Update()
.Set(b => b.Url, “http://sample2222.com”)
.Where(b => b.Url == “http://sample.com”)
.ExecuteAffrows();
查:
var blogs = fsql.Select()
.Where(b => b.Rating > 3)
.OrderBy(b => b.Url)
.Skip(100)
.Limit(10) //第100行-110行的记录
.ToList();
避坑——唯一键设置
百度了几个文档都是说主键应该这样设置:
public Guid id { get; set; }
[Column(Unique = “uk_phone”)]
public string phone { get; set; }
[Column(Unique = "uk_group_index, uk_group_index22")]
public string group { get; set; }
但是上面这么写一定会报错
而正确的写法应该是这样:这里附上连接以供查询:https://www.cnblogs.com/FreeSql/p/11531302.html,该网站里面还描述了主键属性等其他用法,特此分享给大家。
[Index(“uk_phone”, “phone”, true)]
[Index(“uk_group_index”, “group,index”, true)]
[Index(“uk_group_index22”, “group, index22 desc”, true)]
class AddUniquesInfo
{
public Guid id { get; set; }
public string phone { get; set; }
public string group { get; set; }
public int index { get; set; }
public string index22 { get; set; }
}