小编kay*_*tea的帖子

实体框架核心DbContext.RemoveRange和类型约束

此代码抛出异常

System.InvalidOperationException:找不到实体类型"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);
}
Run Code Online (Sandbox Code Playgroud)

但是,如果添加类型约束,则where T: class不会引发异常.为什么是这样?我的印象是C#类型约束没有影响这样的运行时行为.两个版本编译都很好.

c# generics entity-framework-core

4
推荐指数
1
解决办法
305
查看次数

标签 统计

c# ×1

entity-framework-core ×1

generics ×1