【发布时间】:2020-05-06 17:49:27
【问题描述】:
我们正在尝试批量更新(EFCore.BulkExtensions)基于主键的表。我们只需要根据Id 更新Name 而不是Age
型号
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.
那么我如何 BulkUpdate(EFCore.BulkExtensions) 基于主键的表。
【问题讨论】:
标签: c# entity-framework-core efcore.bulkextensions