【问题标题】:Why is Visual Studio telling me I need to reference System.Private.CoreLib?为什么 Visual Studio 告诉我需要引用 System.Private.CoreLib?
【发布时间】:2017-05-25 14:35:07
【问题描述】:

我是第一次尝试 EF Core,并且编写了一个非常简单的 MVC 应用程序来让我的脚湿透。我正在使用一种方法为UnicornStore project 中的数据库播种,他们在Startup.cs 中编写了一些代码来迁移数据库,然后运行种子方法。

在他们调用种子方法之前,他们运行这个DbContext扩展方法来检查是否所有的迁移都被应用了:

using System;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;

namespace UnicornStore.Models
{
    public static class DbContextExtensions
    {
        public static bool AllMigrationsApplied(this DbContext context)
        {
            var applied = context.GetService<IHistoryRepository>()
                .GetAppliedMigrations()
                .Select(m => m.MigrationId);

            var total = context.GetService<IMigrationsAssembly>()
                .Migrations
                .Select(m => m.Key);

            return !total.Except(applied).Any();
        }
    }
}

我已经在我的应用程序中使用了同样的方法并且一切正常——代码编译并且数据库被迁移和播种。但是,Visual Studio (2017 Enterprise) 是红色下划线的:

context.GetService<IMigrationsAssembly>()
                    .Migrations
                    .Select(m => m.Key);

如果我将鼠标悬停在红线上,它会告诉我:

模块'System.Private.CoreLib,版本=4.0.0.0,文化=中性, PublicKeyToken=foo' 应该被引用

谁能告诉我为什么我会收到这条消息?我实际上尝试添加对System.Private.CoreLib 的引用以查看会发生什么,这导致了大量错误(未定义的System.Object 等)。如果他们以后再来咬我,我总是不愿意让这样的事情悬而未决,所以任何解决方案(或确认我可以留下这个并忽略该消息)将不胜感激!

【问题讨论】:

    标签: visual-studio asp.net-core asp.net-core-mvc entity-framework-core


    【解决方案1】:

    您是否安装了 R#?你可能遇到过这个问题:RSRP-464676

    如果是这样,请尝试暂停 R# 并查看问题是否不再显示。

    【讨论】:

      【解决方案2】:

      作为替代响应,似乎我的happy-go-lucky alt-enter 垃圾邮件让我导入了对System.Private.CoreLib 的程序集引用,我认为这是来自线程命名空间导入修复。检查是否已被引用。

      【讨论】:

        【解决方案3】:

        我遇到了同样的问题。升级到最新版本的 Resharper 解决了这个问题。

        【讨论】:

          【解决方案4】:

          在使用 SAFE-Stack 的服务器模板代码的 VS Code (vscode) 中遇到了类似的问题。通过将&lt;Reference include="netstandard" /&gt; 添加到关联的fsproj 项目文件sugggested on github here 中,问题似乎得到了解决,如下所示:

          <Project Sdk="Microsoft.NET.Sdk.Web">
            ..
            <ItemGroup>
              ..
              <Reference include="netstandard" />
              ..
            </ItemGroup>
          </Project>
          

          【讨论】:

            猜你喜欢
            • 2010-12-12
            • 2015-02-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-01-14
            • 1970-01-01
            • 1970-01-01
            • 2011-04-23
            相关资源
            最近更新 更多