首先定义一个DbContext的扩展类DbContextDetachAllExtension,其中包含一个DbContext的扩展方法DetachAll,用来取消跟踪DbContext中所有被跟踪的实体: using Microsoft.EntityFrameworkCore; using System.Linq; namespace DbContextUtils { /// <summary> /// DbContext的扩展类 /// </summary> public sta
下面是EF Core中的一个Person实体: public partial class Person { public int Id { get; set; } public string Code { get; set; } public string Name { get; set; } public DateTime? CreateTime { get; set; } public DateTime? UpdateTime { get; set; } } 其中我们通过Fluent API
假设在数据库中有两个表:Person表和Book表,Person和Book是一对多关系 Person表数据: Book表数据: 可以看到数据库Book表中所有的数据都属于Person表中"F"这个人 Person表,下面的Person类是该表在EF Core中的实体类型: public partial class Person { public Person() { Book = new HashSet<Book>(); } public int Id { get; set
1批量添加 db.T_Investigator.AddRange(list) 2批量删除 db.T_Investigator.RemoveRange(list) 3批量修改 for 循环修改. 注意: 先查询出来,最后savechange(),写在一个事务中,一次请求一个上下文. public int SubmitAll(int[] idList, int userId) { int num = 0; using (var pmdb = new ProjectManagement
在EF各版本中,没有相应批量的添加,删除,修改,在用ORM 处理数据时直有个尴尬.在网上,也接到了很多网友的询问这方面的情况,特此今天把这方面的相关扩展分享一下,(这里只做批量删除的例子,添加和修改的思路雷同) 一.原由 在先前的SQL操作中,我们要 update table set cl=1 where id>5 and id<100 delete from table where id>5 and id<100 但是在EF中没有提供相应的接口,我们只能这样 //批量删除fore