【问题标题】:Both Entity Framework Core and Entity Framework 6 are installedEntity Framework Core 和 Entity Framework 6 都安装了
【发布时间】:2017-08-05 02:28:45
【问题描述】:

我使用 Visual Studio 2017 并创建一个新的 ASP.NET Core 项目。 我为 Entity Framework Core 添加迁移如下

add-migration "Initial Create"

并得到以下错误:

Both Entity Framework Core and Entity Framework 6 are installed. The Entity Framework Core tools are running. Use 'EntityFramework\Add-Migration' for Entity Framework 6.
Build Failed.

你知道如何解决这个错误吗?


更新 1 - 2017 年 3 月 14 日 - 运行命令后收到结果

PM> dotnet ef migrations add "Initial Create"

我看到以下输出,最后带有错误消息。

Welcome to .NET Core!
---------------------
Learn more about .NET Core @ https://aka.ms/dotnet-docs. Use dotnet --help to see available commands or go to https://aka.ms/dotnet-cli-docs.

Telemetry
--------------
The .NET Core tools collect usage data in order to improve your experience. The data is anonymous and does not include command-line arguments. The
 data is collected by Microsoft and shared with the community.
You can opt out of telemetry by setting a DOTNET_CLI_TELEMETRY_OPTOUT environment variable to 1 using your favorite shell.
You can read more about .NET Core tools telemetry @ https://aka.ms/dotnet-cli-telemetry.

Configuring...
-------------------
A command is running to initially populate your local package cache, to improve restore speed and enable offline access. This command will take up
 to a minute to complete and will only happen once.
Decompressing 0%Decompressing 1%Decompressing 2%Decompressing 3%Decompressing 4%
Decompressing 5%Decompressing 6%Decompressing 7%Decompressing 8%Deco
mpressing 9%Decompressing 10%Decompressing 11%Decompressing 12%Decompressing 13
%Decompressing 14%Decompressing 15%Decompressing 16%Decompressing 17%
Decompressing 18%Decompressing 19%Decompressing 20%Decompressing 21%De
compressing 22%Decompressing 23%Decompressing 24%Decompressing 25%Decompressin
g 26%Decompressing 27%Decompressing 28%Decompressing 29%Decompressing 30%
Decompressing 31%Decompressing 32%Decompressing 33%Decompressing 34%
Decompressing 35%Decompressing 36%Decompressing 37%Decompressing 38%Decompre
ssing 39%Decompressing 40%Decompressing 41%Decompressing 42%Decompressing 43%
Decompressing 44%Decompressing 45%Decompressing 46%Decompressing 47%
Decompressing 48%Decompressing 49%Decompressing 50%Decompressing 51%Deco
mpressing 52%Decompressing 53%Decompressing 54%Decompressing 55%Decompressing 
56%Decompressing 57%Decompressing 58%Decompressing 59%Decompressing 60%
Decompressing 61%Decompressing 62%Decompressing 63%Decompressing 64%
Decompressing 65%Decompressing 66%Decompressing 67%Decompressing 68%Decompress
ing 69%Decompressing 70%Decompressing 71%Decompressing 72%Decompressing 73%
Decompressing 74%Decompressing 75%Decompressing 76%Decompressing 77%
Decompressing 78%Decompressing 79%Decompressing 80%Decompressing 81%Decomp
ressing 82%Decompressing 83%Decompressing 84%Decompressing 85%Decompressing 86
%Decompressing 87%Decompressing 88%Decompressing 89%Decompressing 90%
Decompressing 91%Decompressing 92%Decompressing 93%Decompressing 94%De
compressing 95%Decompressing 96%Decompressing 97%Decompressing 98%Decompressin
g 99%Decompressing 100% 5083 ms
Expanding 0%Expanding 1%Expanding 2%Expanding 3%Expanding 4%Expanding 5%Ex
panding 6%Expanding 7%Expanding 8%Expanding 9%Expanding 10%Expanding 11%
Expanding 12%Expanding 13%Expanding 14%Expanding 15%Expanding 16%Expanding 17%
Expanding 18%Expanding 19%Expanding 20%Expanding 21%Expanding 22%Expand
ing 23%Expanding 24%Expanding 25%Expanding 26%Expanding 27%Expanding 28%
Expanding 29%Expanding 30%Expanding 31%Expanding 32%Expanding 33%Expanding 34
%Expanding 35%Expanding 36%Expanding 37%Expanding 38%Expanding 39%Ex
panding 40%Expanding 41%Expanding 42%Expanding 43%Expanding 44%Expanding 45%
Expanding 46%Expanding 47%Expanding 48%Expanding 49%Expanding 50%Expandin
g 51%Expanding 52%Expanding 53%Expanding 54%Expanding 55%Expanding 56%
Expanding 57%Expanding 58%Expanding 59%Expanding 60%Expanding 61%Expanding 62%
Expanding 63%Expanding 64%Expanding 65%Expanding 66%Expanding 67%Expa
nding 68%Expanding 69%Expanding 70%Expanding 71%Expanding 72%Expanding 73%
Expanding 74%Expanding 75%Expanding 76%Expanding 77%Expanding 78%Expanding 
79%Expanding 80%Expanding 81%Expanding 82%Expanding 83%Expanding 84%
Expanding 85%Expanding 86%Expanding 87%Expanding 88%Expanding 89%Expanding 90%
Expanding 91%Expanding 92%Expanding 93%Expanding 94%Expanding 95%Expand
ing 96%Expanding 97%Expanding 98%Expanding 99%Expanding 100% 13884 ms


dotnet : No executable found matching command "dotnet-ef"
At line:1 char:1
+ dotnet ef migrations add "Initial Create"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (No executable f...and "dotnet-ef":String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

更新 2 - 2017 年 3 月 14 日 - Startup.cs

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Configuration;
using OdeToFood.Services;
using OdeToFood.Entities;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;

namespace OdeToFood
{
    public class Startup
    {
        public IConfiguration Configuration { get; set; }

        public Startup(IHostingEnvironment env)
        {
            // TN - Read setting files
            var builder = new ConfigurationBuilder()
                            .SetBasePath(env.ContentRootPath)
                            .AddJsonFile("appsettings.json")
                            .AddEnvironmentVariables();

            Configuration = builder.Build();
        }

        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            services.AddSingleton(Configuration);
            services.AddSingleton<IGreeter, Greeter>();
            services.AddScoped<IRestaurantData, SqlRestaurantData>(); // TN - One instance of this service for each HTTP request.
            services.AddDbContext<OdeToFoodDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
            services.AddIdentity<User, IdentityRole>()
                .AddEntityFrameworkStores<OdeToFoodDbContext>();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                //app.UseExceptionHandler("/error");
                //app.UseExceptionHandler(new ExceptionHandlerOptions { ExceptionHandlingPath="/error" });

                app.UseExceptionHandler(new ExceptionHandlerOptions
                {
                    ExceptionHandler = context => context.Response.WriteAsync("Opps!")
                });
            }

            //app.UseDefaultFiles();// TN - will pick index.html
            //app.UseStaticFiles();

            app.UseFileServer(); // TN - This will include UseDefaultFiles() and UseStaticFiles

            /*
            app.UseWelcomePage(new WelcomePageOptions
            {
                Path = "/welcome"
            });

            app.Run(async (context) =>
            {
                // TN - Read directly from configuration file
                //var message = Configuration["Greeting"];

                // TN - Dependency Injection - Read from configuration string via IOC
                var message = greeter.GetGreeting();
                await context.Response.WriteAsync(message);
            });
            */


            app.UseIdentity();

            //app.UseMvcWithDefaultRoute();
            app.UseMvc(ConfigureRoutes);

            // TN - if no route matches
            app.Run(ctx => ctx.Response.WriteAsync("Not found."));
        }

        private void ConfigureRoutes(IRouteBuilder routeBuilder)
        {
            //Home/Index
            routeBuilder.MapRoute("Default",
                "{controller=Home}/{action=Index}/{id?}");
        }
    }
}

更新 3 - 2017 年 3 月 14 日 - 添加了 csproj 文件

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup Label="Globals">
    <SccProjectName>SAK</SccProjectName>
    <SccProvider>SAK</SccProvider>
    <SccAuxPath>SAK</SccAuxPath>
    <SccLocalPath>SAK</SccLocalPath>
  </PropertyGroup>

  <PropertyGroup>
    <TargetFramework>netcoreapp1.1</TargetFramework>
    <PackageTargetFallback>portable-net45+win8</PackageTargetFallback>
  </PropertyGroup>

  <ItemGroup>
    <!--<Content Include="wwwroot\index.html" />-->
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.0" />
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.0" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.0" />
    <PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
    <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.1.0" />
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.1" />
    <PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="1.1.0" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="1.1.0" />
  </ItemGroup>
  <ItemGroup>
    <Folder Include="Controllers\" />
    <Folder Include="Data\" />
    <Folder Include="Views\Home\" />
    <Folder Include="wwwroot\images\" />
  </ItemGroup>

</Project>

【问题讨论】:

  • 您发布的第一个错误(关于安装了 EF6)不是错误。这只是一个警告。应该从该命令成功添加 EFCore 迁移。除了警告之外还有其他问题吗?
  • 我收到“构建失败”。该警告后的消息。
  • 当 EF Core 执行任何命令时,首先它会构建项目。错误消息表明无法构建项目。尝试在 Visual Studio 中构建项目并查看错误列表窗口以查看构建错误。此故障与EF具体无关。

标签: asp.net-core entity-framework-core


【解决方案1】:

要为 EF7 添加迁移,请使用 dotnet 命令:dotnet ef migrations add Initial
要更新您的数据库,请使用 dotnet 命令:dotnet ef database update

【讨论】:

  • 感谢 Agua,我已经用“dotnet ef migrations add Initial”命令的结果更新了我的问题。你知道如何解决这个错误吗?
  • 您是否尝试从类库添加迁移?
  • 不,我从新的 Asp.net Core Web 应用程序添加迁移。
  • 你能发布你的 Startup.cs 吗?
  • 并在您的 csproj 中找到
【解决方案2】:

也许您的解决方案中至少有两个项目,每个项目都使用任一版本的 EF。通过发出“Manage NuGet packages for solution...”,您可以验证是否是这种情况。然后,也许您需要牺牲使用 EF 6.x 的那个。之后你需要重新启动VS,也许一些缓存会阻止你卸载它时完全删除它。

编辑:.. 或者你可以和他们相处得很好,但是仔细阅读消息并按照它所说的......

【讨论】:

  • 这就是我所需要的。在我的案例中,诀窍是在从解决方案中包含它的几个项目中删除 EF6 后,我没有重新启动 Visual Studio。即使没有更多的项目有 EF6,只有 EFCore,我仍然收到消息。重新启动 Visual Studio 使其消失。
  • 对我来说也一样。重启VS修复它。
  • @Alexander_Christov 请详细说明您的答案。这个答案不是很清楚。
【解决方案3】:

我如何在 VS 2017 中解决这个问题

  • 从解决方案中的所有项目中删除 EntityFramework(不是 EntityFrameworkCore)引用
    • 在资源管理器(Windows 资源管理器)中打开解决方案文件夹
    • 关闭VS
    • 删除文件夹
    • 再次打开解决方案
    • 运行update-database,警报就消失了

您没有看到“.vs”文件夹?

【讨论】:

    【解决方案4】:

    只是……

    编辑您的项目(项目 -> 右键 -> 编辑)并删除 EntityFramewok Line

    删除一行

        <PackageReference Include="Entityframework" Version="6.2.0" />
    

    并且只使用 EntityFramework Core

    【讨论】:

      【解决方案5】:

      如果同时安装了 Entity Framework Core 和 Entity Framework 6,并且您想使用 Entity Framework 6。
      在包管理器控制台中使用此命令:

      'EntityFramework\Update-Database'.
      

      无需删除或更改任何内容。只需在您的 EF6 命令之前添加 **EntityFramework**。

      【讨论】:

        【解决方案6】:

        对于那些使用Entity Framework 6.3 and later to .Net Core 新端口的用户,解决方案很有可能同时包含 EfCore 和 Ef6 工件。

        例如我已将较旧的 EF6 存储库从 .Net Framework 迁移到 Core,但想利用开箱即用的 AspNet Identity Core 子系统,它使用 EfCore。

        在迁移过程中,我总是会收到如下错误:

        Entity Framework Core 和 Entity Framework 6 都已安装。 Entity Framework Core 工具正在运行。对 Entity Framework 6 使用“EntityFramework6\Update-Database”。

        Entity Framework 6 和 Entity Framework Core 都已安装。 Entity Framework 6 工具正在运行。对 Entity Framework Core 使用“EntityFrameworkCore\Add-Migration”

        解决方案很简单 - 在运行迁移命令时,根据警告,始终使用完全限定的命令来限定命令:

        EntityFramework6\Add-Migration Foo
        EntityFramework6\Update-Database
        

        EntityFrameworkCore\Add-Migration Foo
        EntityFrameworkCore\Update-Database
        

        请注意,对于 EF6 粉丝来说,在撰写本文时,移植到 Core 的迁移工具还没有完全达到标准,正如 here 所说。

        同样,如果您即使在同一个框架版本中也有多个 DbContext(EfCore 与 Ef6 on Core),您需要指定您尝试定位的特定 DbContext 的名称,例如

        EntityFramework6\Add-Migration Foo -Context BarDbContext
        EntityFramework6\Add-Migration Baz -Context FooDbContext
        

        【讨论】:

          猜你喜欢
          • 2019-01-11
          • 1970-01-01
          • 2017-04-04
          • 2021-12-22
          • 1970-01-01
          • 1970-01-01
          • 2018-02-23
          • 1970-01-01
          • 2023-03-14
          相关资源
          最近更新 更多