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; }
}
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 });
Ignore column
fsql.Insert<Topic>(items).IgnoreColumns(a => a.CreateTime).ExecuteAffrows();
Execute SQL as follows:
INSERT INTO `tb_topic`(`Clicks`, `Title`) VALUES(?Clicks0, ?Title0), (?Clicks1, ?Title1), (?Clicks2, ?Title2), (?Clicks3, ?Title3), (?Clicks4, ?Title4), (?Clicks5, ?Title5), (?Clicks6, ?Title6), (?Clicks7, ?Title7), (?Clicks8, ?Title8), (?Clicks9, ?Title9)
fsql.Insert<Topic>(items).IgnoreColumns(a => new { a.Title, a.CreateTime }).ExecuteAffrows();
Execute SQL as follows:
INSERT INTO `tb_topic`(`Clicks`) VALUES(?Clicks0), (?Clicks1), (?Clicks2), (?Clicks3), (?Clicks4), (?Clicks5), (?Clicks6), (?Clicks7), (?Clicks8), (?Clicks9)
API
method | return value | parameter | description |
---|---|---|---|
AppendData | <this> | T1 | IEnumerable
|
Additional entities intended for insertion |
InsertIdentity | <this> | no | Specify the insertion auto-increment |
InsertColumns | <this> | Lambda | Just insert columns |
IgnoreColumns | <this> | Lambda | Ignore column |
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 | |
ExecuteIdentity | long | Execute SQL statement that returns from value-added | |
ExecuteInserted | List<T1> | Execute SQL statements, after the insertion of records returned |