Visual studio 2019 年上次更新后不再在 select 菜单中显示数据上下文 class

Visual studio 2019 after last update no longer showing the Data context class in the select menu

编辑: 更新 Visual studio 2019 至版本 16.6.3 解决问题

Visual studio 2019 年上次更新后不再显示数据上下文 class、

要重现问题,只需创建一个基于 .net core 3.1 的新 MCV,问题就会出现

暂时解决此问题的最佳方法是创建另一个扩展 DbContext 的数据库上下文

  • 先创建一个class命名为WorkAroundContext.cs,代码如下

     using Microsoft.EntityFrameworkCore;
     using System;
     using System.Collections.Generic;
     using System.Linq;
      namespace YourNameSpace
     {
    
       public partial class WorkAroundContext : DbContext
       {
         public WorkAroundContext(DbContextOptions<WorkAroundContext> options)
               : base(options)
         { }
       }
     }
    
  • 然后在你的 startup.cs 添加以下代码

      services.AddDbContext<WorkAroundContext>(options => options.UseSqlServer(conn));
    
  • 使用 WorkAroundContext 创建控制器

  • 最后编辑生成的文件,将WorkAroundContext改成ApplicationDbContext

编辑: 更新Visual studio 2019 至版本16.6.3 解决问题