C# .Net-添加迁移命令使NuGet包无效
我已安装了最新版本的Microsoft.EntityFrameworkCore,设置了模型,设置了上下文,并已将上下文添加到startup.cs文件中。但是,当我运行命令“dotnet ef migrations add InitialCreate”时,它会显示启动生成,然后停止,并显示以下消息“build started…build failed。使用dotnet build查看错误。” 然后,它似乎使最近安装的NuGet软件包Microsoft.EntityFrameworkCore无效,说它找不到它,也无法链接它的任何相关语法(所有相关代码下面再次出现红线)。尽管之前正确安装了该软件包,但仍然存在这种情况 给出了以下错误:C# .Net-添加迁移命令使NuGet包无效,c#,.net,asp.net-mvc,entity-framework,entity-framework-migrations,C#,.net,Asp.net Mvc,Entity Framework,Entity Framework Migrations,我已安装了最新版本的Microsoft.EntityFrameworkCore,设置了模型,设置了上下文,并已将上下文添加到startup.cs文件中。但是,当我运行命令“dotnet ef migrations add InitialCreate”时,它会显示启动生成,然后停止,并显示以下消息“build started…build failed。使用dotnet build查看错误。” 然后,它似乎使最近安装的NuGet软件包Microsoft.EntityFrameworkCore无效,说
Data\EventContext.cs(5,17): error CS0234: The type or namespace name 'EntityFrameworkCore' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?) [C:\Users\USER\source\repos\EventPlanner\EventPlanner\EventPlanner.csproj]
Data\EventContext.cs(10,33): error CS0246: The type or namespace name 'DbContext' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\USER\source\repos\EventPlanner\EventPlanner\EventPlanner.csproj]
Data\EventContext.cs(16,16): error CS0246: The type or namespace name 'DbSet<>' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\USER\source\repos\EventPlanner\EventPlanner\EventPlanner.csproj]
Data\EventContext.cs(17,16): error CS0246: The type or namespace name 'DbSet<>' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\USER\source\repos\EventPlanner\EventPlanner\EventPlanner.csproj]
Data\EventContext.cs(18,16): error CS0246: The type or namespace name 'DbSet<>' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\USER\source\repos\EventPlanner\EventPlanner\EventPlanner.csproj]
Data\EventContext.cs(12,29): error CS0246: The type or namespace name 'DbContextOptions<>' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\USER\source\repos\EventPlanner\EventPlanner\EventPlanner.csproj]
0 Warning(s)
6 Error(s)
Data\EventContext.cs(5,17):错误CS0234:命名空间“Microsoft”中不存在类型或命名空间名称“EntityFrameworkCore”(是否缺少程序集引用?[C:\Users\USER\source\repos\EventPlanner\EventPlanner\EventPlanner.csproj]
Data\EventContext.cs(10,33):错误CS0246:找不到类型或命名空间名称“DbContext”(是否缺少using指令或程序集引用?[C:\Users\USER\source\repos\EventPlanner\EventPlanner.csproj]
Data\EventContext.cs(16,16):错误CS0246:找不到类型或命名空间名称“DbSet”(是否缺少using指令或程序集引用?[C:\Users\USER\source\repos\EventPlanner\EventPlanner.csproj]
Data\EventContext.cs(17,16):错误CS0246:找不到类型或命名空间名称“DbSet”(是否缺少using指令或程序集引用?[C:\Users\USER\source\repos\EventPlanner\EventPlanner.csproj]
Data\EventContext.cs(18,16):错误CS0246:找不到类型或命名空间名称“DbSet”(是否缺少using指令或程序集引用?[C:\Users\USER\source\repos\EventPlanner\EventPlanner.csproj]
Data\EventContext.cs(12,29):错误CS0246:找不到类型或命名空间名称“DbContextOptions”(是否缺少using指令或程序集引用?[C:\Users\USER\source\repos\EventPlanner\EventPlanner\EventPlanner.csproj]
0个警告
6个错误
有没有人会对为什么会发生这种情况提出建议?我使用的是NuGet软件包Microsoft.EntityFrameworkCore的5.0.6版。我已尝试卸载并重新安装该软件包,红线暂时消失,但只要我运行migration命令,它们就会再次出现
谢谢
罗伯特
英国伦敦
//
上下文类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using EventPlanner.Models;
namespace EventPlanner.Data
{
public class EventContext : DbContext
{
public EventContext(DbContextOptions<EventContext> options) : base(options)
{
}
public DbSet<Event> Events { get; set; }
public DbSet<Band> Bands { get; set; }
public DbSet<Caterer> Caterers { get; set; }
}
}
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
使用Microsoft.EntityFrameworkCore;
使用EventPlanner.Models;
命名空间EventPlanner.Data
{
公共类EventContext:DbContext
{
公共事件上下文(DbContextOptions):基本(选项)
{
}
公共数据库集事件{get;set;}
公共DbSet带{get;set;}
公共数据库集餐饮服务商{get;set;}
}
}
//Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddDbContext<EventContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
}
public void配置服务(IServiceCollection服务)
{
services.AddControllersWithViews();
services.AddDbContext(选项=>
options.UseSqlServer(Configuration.GetConnectionString(“DefaultConnection”));
}
您确定您的项目是.net核心项目,而不是.net framework项目吗?是的。目标框架(在“属性”下)是.Net Core 3.1。