我们正在尝试使用主键批量更新表格 (EFCore.BulkExtensions)。我们需要基于
模型:
Id
而不是 Age
来仅更新 Name
。模型:
public class Student
{
public int Id { get; set; } // Primary Key
public string Name { get; set; }
public int Age { get; set; }
}
这是我用来尝试使用主键 Id 更新学生姓名的代码:
List<Student> students = new List<Student>();
students.Add(new Student()
{
Id = 1,
Name = "Name 1",
Age = 25
});
var updateByProperties = new List<string> { nameof(Student.Id) };
var propToExclude = new List<string> { nameof(Student.Id) };
var bulkConfig = new BulkConfig { UpdateByProperties = updateByProperties, PropertiesToExclude = propToExclude };
_dbContext().BulkUpdate(students, bulkConfig);
我期望它将更新一行中 Id
为1的列 Name
,但是我却遇到了以下错误
The given key 'Id' was not present in the dictionary.
那么我如何基于主键使用EFCore.BulkExtensions批量更新数据表呢?