/images/placeholder.jpg

EFCore出现Unable to resolve service..的解决办法

  • "date" : "2023-08-31 10:33:53"
  • "classfiy" : "ef core"
  • "author" : "飞扬"
  • "viewTimes" : 2148

在.net7使用新搭建机架的项目功能时出现这个问题:

Unable to resolve service for type ‘xxx’ while attempting to activate ‘xxx’.

解决方法

  1. public class BZContext : DbContext
  2. {
  3. //在使用 新搭建机架的项目 时注释掉这个构造函数使用下面的重写方法就行了
  4. public BZContext(DbContextOptions<BZContext> options) : base(options)
  5. {
  6. }
  7. #if DEBUG
  8. //搭建完以后注释掉这里使用上面的方式就行
  9. protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
  10. {
  11. optionsBuilder.UseSqlServer("数据库连接字符串");
  12. }
  13. #endif
  14. }