FreeSql 插入数据,如何返回自增值

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

FreeSql 插入数据,如何返回自增值

FreeSql   2020-03-08 我要评论
FreeSql是一个功能强大的 .NET ORM 功能库,支持 .NetFramework 4.0+、.NetCore 2.1+、Xamarin 等支持 NetStandard 所有运行平台。 以 MIT 开源协议托管于 github:[https://github.com/2881099/FreeSql](https://github.com/2881099/FreeSql) FreeSql 插入数据的方式有多种,这篇文章教你用最优的方案做数据插入功能。 ```csharp static IFreeSql fsql = new FreeSql.FreeSqlBuilder() .UseConnectionString(FreeSql.DataType.Sqlite, "Data Source=db1.db") .UseAutoSyncStructure(true) //自动同步实体结构到数据库 .Build(); //请务必定义成 Singleton 单例模式 public class Blog { [Column(IsIdentity = true, IsPrimary = true)] public int BlogId { get; set; } public string Url { get; set; } public int Rating { get; set; } } var blog = new Blog { Url = "https://github.com/2881099/FreeSql", Rating = 5 }; ``` ### 单条数据插入 如果表有自增列,插入数据后应该要返回 id。 方法1:(原始) ```csharp long id = fsql.Insert(blog).ExecuteIdentity(); blog.Id = id; ``` 方法2:(依赖 FreeSql.Repository) ```csharp var repo = fsql.GetRepository(); repo.Insert(blog); ``` > 将插入后的自增值,填充给 blog.Id 方法3:(依赖 FreeSql.DbContext) ```csharp using (var ctx = fsql.CreateDbContext()) { ctx.Add(blog); ctx.SaveChanges(); } ``` > 将插入后的自增值,填充给 blog.Id ### 批量插入 ```csharp var items = new List(); for (var a = 0; a < 10; a++) { items.Add(new Blog { Url = "https://github.com/2881099/FreeSql", Rating = 5 }); } ``` 方法1:(原始) ```csharp fsql.Insert(items).ExecuteAffrows(); ``` > 无法返回 items 所有 id 值 方法2:(依赖 FreeSql.Repository) ```csharp var repo = fsql.GetRepository(); repo.Insert(items); ``` > 将插入后的自增值,填充给所有 items.Id 当操作的是 SqlServer/PostgreSql 数据库,此方法为一次执行,返回所有 id 当操作的是其他数据库,此方法为循环多次执行,返回所有 id(注意性能问题) ### 大批量插入(SqlBulkCopy、BulkCopy) 针对 SqlServer/PostgreSQL/MySql 数据库,目前能在以下实现使用: - FreeSql.Provider.SqlServer - FreeSql.Provider.PostgreSQL - FreeSql.Provider.MySqlConnector ```csharp fsql.Insert(items).ExecuteSqlBulkCopy(); fsql.Insert(items).ExecutePgCopy(); fsql.Insert(items).ExecuteMySqlBulkCopy(); ``` 另外 IInsert 方法提供了 ToDataTable() 方法返回 DataTable 对象,让使用者自己封装 BulkCopy 操作。 ```csharp DataTable dt = fsql.Insert(items) .InsertIdentity() //开启自增 id 插入 .ToDataTable(); ``` 注意:InsertIdentity() 的功能是生成 SQL 的时候有值,而不是调用 SET IDENTITY ON; ### 参考资料 | | | | - | - | | | [《新人学习指引》](https://www.cnblogs.com/FreeSql/p/11531300.html) \| [《Select》](https://github.com/2881099/FreeSql/wiki/%e6%9f%a5%e8%af%a2) \| [《Update》](https://github.com/2881099/FreeSql/wiki/%e4%bf%ae%e6%94%b9) \| [《Insert》](https://github.com/2881099/FreeSql/wiki/%e6%b7%bb%e5%8a%a0) \| [《Delete》](https://github.com/2881099/FreeSql/wiki/%e5%88%a0%e9%99%a4) | | | [《表达式函数》](https://github.com/2881099/FreeSql/wiki/%e8%a1%a8%e8%be%be%e5%bc%8f%e5%87%bd%e6%95%b0) \| [《CodeFirst》](https://github.com/2881099/FreeSql/wiki/CodeFirst) \| [《DbFirst》](https://github.com/2881099/FreeSql/wiki/DbFirst) \| [《BaseEntity》](https://github.com/2881099/FreeSql/tree/master/Examples/base_entity) | | | [《Repository》](https://github.com/2881099/FreeSql/wiki/Repository) \| [《UnitOfWork》](https://github.com/2881099/FreeSql/wiki/%e5%b7%a5%e4%bd%9c%e5%8d%95%e5%85%83) \| [《过滤器》](https://github.com/2881099/FreeSql/wiki/%e8%bf%87%e6%bb%a4%e5%99%a8) \| [《乐观锁》](https://github.com/2881099/FreeSql/wiki/%e4%bf%ae%e6%94%b9#%E4%B9%90%E8%A7%82%E9%94%81) \| [《DbContext》](https://github.com/2881099/FreeSql/wiki/DbContext) | | | [《读写分离》](https://github.com/2881099/FreeSql/wiki/%e8%af%bb%e5%86%99%e5%88%86%e7%a6%bb) \| [《分区分表》](https://github.com/2881099/FreeSql/wiki/%e5%88%86%e5%8c%ba%e5%88%86%e8%a1%a8) \| [《租户》](https://github.com/2881099/FreeSql/wiki/%e7%a7%9f%e6%88%b7) \| [《AOP》](https://github.com/2881099/FreeSql/wiki/AOP) \| [《黑科技》](https://github.com/2881099/FreeSql/wiki/%E9%AA%9A%E6%93%8D%E4%BD%9C) \| [*更新日志*](https://github.com/2881099/FreeSql/wiki/%e6%9b%b4%e6%96%b0%e6%97%a5%e5%bf%97) |

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们