VS Studio调试时“函数评估需所有线程运行”错误的解决求助
解决Visual Studio调试时
the function evaluation requires all threads to run错误 在Visual Studio调试过程中,遇到错误提示:
the function evaluation requires all threads to run,截图如下:
项目采用EF Code First模式,相关代码如下:
控制器代码
public class ApplicationController : Controller { ApplicationDbContext _context = new ApplicationDbContext(); public ActionResult Index() { List<Brand> brands; brands = _context.Brands.ToList(); return View(brands); } }
DbContext代码
public class ApplicationDbContext : DbContext { public ApplicationDbContext() : base("name=ApplicationDbContext") { } public virtual DbSet<Brand> Brands { get; set; } public virtual DbSet<Product> Products { get; set; } }
Brand模型代码
public class Brand { [Key] public int brand_id { get; set; } [Required(ErrorMessage = "Nhập thiếu kìa fen.")] public string brandname { get; set; } [Required(ErrorMessage = "Nhập thiếu kìa fen.")] public string program { get; set; } [Required(ErrorMessage = "Nhập thiếu kìa fen.")] public string currency { get; set; } public string note { get; set; } [Required(ErrorMessage = "Nhập thiếu kìa fen.")] [DataType(DataType.Date)] [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)] public DateTime timestart { get; set; } [Required(ErrorMessage = "Nhập thiếu kìa fen.")] [DataType(DataType.Date)] [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd-MM-yyyy}")] public DateTime timeend { get; set; } public virtual List<Product> Products { get; set; } = new List<Product>(); }
Product模型代码
public class Product { [Key] public int product_id { get; set; } [ForeignKey("Brand")] public int brand_id { get; set; } public virtual Brand Brand { get; private set; } [Required(ErrorMessage = "Nhập thiếu kìa fen.")] public string productname { get; set; } [Required(ErrorMessage = "Nhập thiếu kìa fen.")] public string condition { get; set; } [Required(ErrorMessage = "Nhập thiếu kìa fen.")] [Column(TypeName = "numeric")] public decimal rebate { get; set; } public string note { get; set; } }
修复方法
禁用“仅我的代码”调试选项
打开Visual Studio的工具->选项->调试->常规,取消勾选启用“仅我的代码”,重启调试。该选项可能干扰EF实体的懒加载评估。处理EF懒加载问题
错误多因调试时访问未加载的关联实体触发,可二选一操作:- 在DbContext构造函数中关闭懒加载:
public ApplicationDbContext() : base("name=ApplicationDbContext") { this.Configuration.LazyLoadingEnabled = false; } - 查询时提前加载关联数据:
brands = _context.Brands.Include(b => b.Products).ToList();
- 在DbContext构造函数中关闭懒加载:
调整调试器函数评估设置
打开工具->选项->调试->常规,取消勾选允许在调试期间对函数进行评估,避免调试器自动评估可能阻塞的EF实体属性。排查线程阻塞
通过调试->窗口->线程查看线程状态,确认是否有线程挂起或死锁,EF查询需要数据库连接线程正常运行,阻塞会触发该错误。
内容的提问来源于stack exchange,提问作者quanhprx
相关产品推荐
相关产品推荐


