C# 我无法在ASP.NET Core中创建迁移。解决办法是什么?
Visual Studio 2019社区。我想创建一个表来创建迁移并连接SQL Server 2014。但是当我跑的时候C# 我无法在ASP.NET Core中创建迁移。解决办法是什么?,c#,sql-server,entity-framework,visual-studio-2019,core,C#,Sql Server,Entity Framework,Visual Studio 2019,Core,Visual Studio 2019社区。我想创建一个表来创建迁移并连接SQL Server 2014。但是当我跑的时候 add-migrations AddMusicStoreToDb 我得到一个错误: 添加迁移:术语“添加迁移”不能识别为cmdlet、函数、脚本文件或可操作程序的名称。请检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试 第1行字符:1 +添加迁移AddMusicStoreToDb +~~~~~~~~~~~~~~~~ +CategoryInfo:ObjectNo
add-migrations AddMusicStoreToDb
我得到一个错误:
添加迁移:术语“添加迁移”不能识别为cmdlet、函数、脚本文件或可操作程序的名称。请检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试
第1行字符:1+添加迁移AddMusicStoreToDb
+~~~~~~~~~~~~~~~~
+CategoryInfo:ObjectNotFound:(添加迁移:字符串)[],CommandNotFoundException
+FullyQualifiedErrorId:CommandNotFoundException
appsettings.json
:
{
"ConnectingStrings": {
"DefaultConnection": "Server=DESKTOP-NHG0GU1\\SQLEXPRESS;Database=MusicStoreList ;Trusted_Connection=True;MultipleActiveResultSets=true;"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
MusicStoreListContext.cs:
using Microsoft.EntityFrameworkCore;
namespace MusicStoreRazor.UI.Models
{
public class MusicStoreListContext:DbContext
{
public MusicStoreListContext(DbContextOptions<MusicStoreListContext> options):base(options)
{
}
public DbSet<Music> Musics { get; set; }
}
}
使用Microsoft.EntityFrameworkCore;
命名空间MusicStoreRazor.UI.Models
{
公共类MusicStoreListContext:DbContext
{
公共MusicStoreListContext(DbContextOptions选项):基本(选项)
{
}
公共数据库集音乐{get;set;}
}
}
Startup.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using MusicStoreRazor.UI.Models;
namespace MusicStoreRazor.UI
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<MusicStoreListContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddRazorPages().AddRazorRuntimeCompilation();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
使用Microsoft.AspNetCore.Builder;
使用Microsoft.AspNetCore.Hosting;
使用Microsoft.AspNetCore.HttpsPolicy;
使用Microsoft.EntityFrameworkCore;
使用Microsoft.Extensions.Configuration;
使用Microsoft.Extensions.DependencyInjection;
使用Microsoft.Extensions.Hosting;
使用Microsoft.Extensions.Options;
使用MusicStoreRazor.UI.Models;
名称空间MusicStoreRazor.UI
{
公营创业
{
公共启动(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
//此方法由运行时调用。请使用此方法将服务添加到容器中。
public void配置服务(IServiceCollection服务)
{
services.AddDbContext(options=>options.UseSqlServer(Configuration.GetConnectionString(“DefaultConnection”));
services.AddRazorPages().AddRazorRuntimeCompilation();
}
//此方法由运行时调用。请使用此方法配置HTTP请求管道。
public void配置(IApplicationBuilder应用程序、IWebHostEnvironment环境)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
其他的
{
app.UseExceptionHandler(“/Error”);
//默认的HSTS值为30天。您可能希望在生产场景中更改此值,请参阅https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(端点=>
{
endpoints.MapRazorPages();
});
}
}
}
您需要在命令前面加上dotnet ef
dotnet ef迁移添加AddMusicStoreToDb
dotnet ef数据库更新
您是否已将这些必需的NuGet软件包安装到解决方案中,以便首先在asp.net核心代码中执行与数据库相关的操作
1。Microsoft.EntityFrameworkCore.SqlServer:提供用于连接SQL Server的类,以便对实体框架核心执行CRUD操作
2。Microsoft.EntityFrameworkCore.Tools:帮助处理与数据库相关的活动,如添加迁移、脚本迁移、获取数据库上下文、更新数据库等
如果没有,请使用软件包管理器控制台或NuGet软件包管理器安装这些软件包。我删除了Entity Framework 6并获得了解决方案,谢谢检查它是否与Microsoft.EntityFrameworkCore.SqlServer和Microsoft.EntityFrameworkCore.Tools的安装没有问题。这两个Entity Framework已安装核心和实体框架6。实体框架核心工具正在运行。对实体框架6使用“EntityFramework6\启用迁移”。启用迁移已过时。使用“添加迁移”开始使用迁移。我还没有尝试添加迁移初始值创建dotnet不可用,dotnet不可用您的命令无效已安装Entity Framework Core和Entity Framework 6。实体框架核心工具正在运行。对实体框架6使用“EntityFramework6\启用迁移”。启用迁移已过时。使用添加迁移开始使用迁移。这不是针对VSCode而不是Visual Studio吗?不确定社区版使用哪一个,因为我有专业版。您可以在Visual Studio的命令窗口中键入。在VS代码中,您可以在终端窗口中键入。