这段代码会抛出异常
系统无法找到实体类型“List<..>”。请确保已将实体类型添加到模型中。
private static void Update<T>(DbContext context, ICollection<T> existing, ICollection<T> updated) // where T: class
{
context.RemoveRange(existing);
updated.ToList().ForEach(existing.Add);
}
然而,如果您添加类型约束
where T: class
,就不会抛出异常。为什么会这样呢?我原本以为C#类型约束不会对运行时行为产生影响。两个版本都可以成功编译。