asp.net核心项目中的常量错误

问题描述 投票:0回答:1

我通过cli创建了一个新的asp.net项目,如下所示:dotnet new Angular。

所以我可以运行该网站。现在我添加了一个dbContext,如下所示:

 public void ConfigureServices(IServiceCollection services)
 {
    services.AddDbContext<VegaDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("Default")));
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

    // In production, the Angular files will be served from this directory
    services.AddSpaStaticFiles(configuration =>
    {
       configuration.RootPath = "ClientApp/dist";
    });
 }

但现在我不能再运行该应用程序了,因为我收到了这些错误:

D:\ Mijn Documents \ UDEMY \ aspnetAngular \ aspnetAngular.csproj:error NU1107:检测到Microsoft.Extensions.Caching.Abstractions的版本冲突。直接安装/引用Microsoft.Extensions.Caching.Abstractions 2.2.0到项目aspnetAngular来解决此问题。 D:\ Mijn Documents \ UDEMY \ aspnetAngular \ aspnetAngular.csproj:错误NU1107:aspnetAngular - > Microsoft.EntityFrameworkCore 2.2.2 - > Microsoft.Extensions.Caching.Memory 2.2.0 - > Microsoft.Extensions.Caching.Abstractions(> = 2.2.0)D:\ Mijn Documents \ UDEMY \ aspnetAngular \ aspnetAngular.csproj:错误NU1107:aspnetAngular - > Microsoft.AspNetCore.App 2.1.1 - > Microsoft.Extensions.Caching.Abstractions(> = 2.1.1 && <2.2 .0)。

The build failed. Please fix the build errors and run again.

这就是我的csproj文件的样子:

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

  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>
    <TypeScriptToolsVersion>Latest</TypeScriptToolsVersion>
    <IsPackable>false</IsPackable>
    <SpaRoot>ClientApp\</SpaRoot>
    <DefaultItemExcludes>$(DefaultItemExcludes);$(SpaRoot)node_modules\**</DefaultItemExcludes>

    <!-- Set this to true if you enable server-side prerendering -->
    <BuildServerSideRenderer>false</BuildServerSideRenderer>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" />
    <PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="2.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="2.2.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Analyzers" Version="2.2.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="2.2.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.sqlServer" Version="2.2.2" />
  </ItemGroup>

  <ItemGroup>
    <!-- Don't publish the SPA source files, but do show them in the project files list -->
    <Content Remove="$(SpaRoot)**" />
    <None Include="$(SpaRoot)**" Exclude="$(SpaRoot)node_modules\**" />
  </ItemGroup>

  <Target Name="DebugEnsureNodeEnv" BeforeTargets="Build" Condition=" '$(Configuration)' == 'Debug' And !Exists('$(SpaRoot)node_modules') ">
    <!-- Ensure Node.js is installed -->
    <Exec Command="node --version" ContinueOnError="true">
      <Output TaskParameter="ExitCode" PropertyName="ErrorCode" />
    </Exec>
    <Error Condition="'$(ErrorCode)' != '0'" Text="Node.js is required to build and run this project. To continue, please install Node.js from https://nodejs.org/, and then restart your command prompt or IDE." />
    <Message Importance="high" Text="Restoring dependencies using 'npm'. This may take several minutes..." />
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm install" />
  </Target>

  <Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
    <!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm install" />
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm run build -- --prod" />
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm run build:ssr -- --prod" Condition=" '$(BuildServerSideRenderer)' == 'true' " />

    <!-- Include the newly-built files in the publish output -->
    <ItemGroup>
      <DistFiles Include="$(SpaRoot)dist\**; $(SpaRoot)dist-server\**" />
      <DistFiles Include="$(SpaRoot)node_modules\**" Condition="'$(BuildServerSideRenderer)' == 'true'" />
      <ResolvedFileToPublish Include="@(DistFiles->'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">
        <RelativePath>%(DistFiles.Identity)</RelativePath>
        <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
      </ResolvedFileToPublish>
    </ItemGroup>
  </Target>

</Project>

所以我的问题是:如何解决这个问题?

因为当我安装de missing库时看起来我是在循环中 - Microsoft.Extensions.Caching.Abstractions 2.2.0

我这样做。然后我会得到另一个错误。

谢谢

哦,我安装了donet版.NET Core 2.2 SDK - Windows x64安装程序(v2.2.104)

但我仍然得到这个错误:

Starting OmniSharp server at 2/22/2019, 4:58:53 PM
    Target: d:\Mijn Documents\UDEMY\aspnetAngular

OmniSharp server started.
    Path: C:\Users\User.USER-PC\.vscode\extensions\ms-vscode.csharp-1.17.1\.omnisharp\1.32.8\OmniSharp.exe
    PID: 12764

[info]: OmniSharp.Stdio.Host
        Starting OmniSharp on Windows 6.2.9200.0 (x64)
[info]: OmniSharp.Services.DotNetCliService
        DotNetPath set to dotnet
[info]: OmniSharp.MSBuild.Discovery.MSBuildLocator
        Located 3 MSBuild instance(s)
            1: Visual Studio Community 2017 15.9.28307.105 - "D:\Program Files (x86)\VisualStudio_2017\MSBuild\15.0\Bin"
            2: Visual Studio Enterprise 2017 15.0.26228.64 - "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin"
            3: StandAlone 15.0 - "C:\Users\User.USER-PC\.vscode\extensions\ms-vscode.csharp-1.17.1\.omnisharp\1.32.8\msbuild\15.0\Bin"
[info]: OmniSharp.MSBuild.Discovery.MSBuildLocator
        Registered MSBuild instance: Visual Studio Community 2017 15.9.28307.105 - "D:\Program Files (x86)\VisualStudio_2017\MSBuild\15.0\Bin"
[info]: OmniSharp.Cake.CakeProjectSystem
        Detecting Cake files in 'd:\Mijn Documents\UDEMY\aspnetAngular'.
[info]: OmniSharp.Cake.CakeProjectSystem
        Could not find any Cake files
[info]: OmniSharp.WorkspaceInitializer
        Project system 'OmniSharp.DotNet.DotNetProjectSystem' is disabled in the configuration.
[info]: OmniSharp.MSBuild.ProjectSystem
        No solution files found in 'd:\Mijn Documents\UDEMY\aspnetAngular'
[info]: OmniSharp.MSBuild.ProjectManager
        Queue project update for 'd:\Mijn Documents\UDEMY\aspnetAngular\aspnetAngular.csproj'
[info]: OmniSharp.Script.ScriptProjectSystem
        Detecting CSX files in 'd:\Mijn Documents\UDEMY\aspnetAngular'.
[info]: OmniSharp.Script.ScriptProjectSystem
        Could not find any CSX files
[info]: OmniSharp.WorkspaceInitializer
        Invoking Workspace Options Provider: OmniSharp.Roslyn.CSharp.Services.CSharpWorkspaceOptionsProvider
[info]: OmniSharp.MSBuild.ProjectManager
        Loading project: d:\Mijn Documents\UDEMY\aspnetAngular\aspnetAngular.csproj
[info]: OmniSharp.WorkspaceInitializer
        Configuration finished.
[info]: OmniSharp.Stdio.Host
        Omnisharp server running using Stdio at location 'd:\Mijn Documents\UDEMY\aspnetAngular' on host 14556.
[warn]: OmniSharp.MSBuild.ProjectLoader
        Detected package version outside of dependency constraint: Microsoft.AspNetCore.App 2.1.1 requires Microsoft.EntityFrameworkCore (>= 2.1.1 && < 2.2.0) but version Microsoft.EntityFrameworkCore 2.2.2 was resolved.
[warn]: OmniSharp.MSBuild.ProjectLoader
        Detected package version outside of dependency constraint: Microsoft.AspNetCore.App 2.1.1 requires Microsoft.EntityFrameworkCore.Abstractions (>= 2.1.1 && < 2.2.0) but version Microsoft.EntityFrameworkCore.Abstractions 2.2.2 was resolved.
[warn]: OmniSharp.MSBuild.ProjectLoader
        Detected package version outside of dependency constraint: Microsoft.AspNetCore.App 2.1.1 requires Microsoft.EntityFrameworkCore.Analyzers (>= 2.1.1 && < 2.2.0) but version Microsoft.EntityFrameworkCore.Analyzers 2.2.2 was resolved.
[warn]: OmniSharp.MSBuild.ProjectLoader
        Detected package version outside of dependency constraint: Microsoft.AspNetCore.App 2.1.1 requires Microsoft.EntityFrameworkCore.Relational (>= 2.1.1 && < 2.2.0) but version Microsoft.EntityFrameworkCore.Relational 2.2.2 was resolved.
[warn]: OmniSharp.MSBuild.ProjectLoader
        Detected package version outside of dependency constraint: Microsoft.AspNetCore.App 2.1.1 requires Microsoft.EntityFrameworkCore.SqlServer (>= 2.1.1 && < 2.2.0) but version Microsoft.EntityFrameworkCore.SqlServer 2.2.2 was resolved.
[fail]: OmniSharp.MSBuild.ProjectLoader
        Version conflict detected for Microsoft.Extensions.Caching.Abstractions. Install/reference Microsoft.Extensions.Caching.Abstractions 2.2.0 directly to project aspnetAngular to resolve this issue. 
 aspnetAngular -> Microsoft.EntityFrameworkCore 2.2.2 -> Microsoft.Extensions.Caching.Memory 2.2.0 -> Microsoft.Extensions.Caching.Abstractions (>= 2.2.0) 
 aspnetAngular -> Microsoft.AspNetCore.App 2.1.1 -> Microsoft.Extensions.Caching.Abstractions (>= 2.1.1 && < 2.2.0).
[warn]: OmniSharp.MSBuild.ProjectManager
        Failed to load project file 'd:\Mijn Documents\UDEMY\aspnetAngular\aspnetAngular.csproj'.
d:\Mijn Documents\UDEMY\aspnetAngular\aspnetAngular.csproj
d:\Mijn Documents\UDEMY\aspnetAngular\aspnetAngular.csproj(0,0): Error: Detected package version outside of dependency constraint: Microsoft.AspNetCore.App 2.1.1 requires Microsoft.EntityFrameworkCore (>= 2.1.1 && < 2.2.0) but version Microsoft.EntityFrameworkCore 2.2.2 was resolved.
d:\Mijn Documents\UDEMY\aspnetAngular\aspnetAngular.csproj(0,0): Error: Detected package version outside of dependency constraint: Microsoft.AspNetCore.App 2.1.1 requires Microsoft.EntityFrameworkCore.Abstractions (>= 2.1.1 && < 2.2.0) but version Microsoft.EntityFrameworkCore.Abstractions 2.2.2 was resolved.
d:\Mijn Documents\UDEMY\aspnetAngular\aspnetAngular.csproj(0,0): Error: Detected package version outside of dependency constraint: Microsoft.AspNetCore.App 2.1.1 requires Microsoft.EntityFrameworkCore.Analyzers (>= 2.1.1 && < 2.2.0) but version Microsoft.EntityFrameworkCore.Analyzers 2.2.2 was resolved.
d:\Mijn Documents\UDEMY\aspnetAngular\aspnetAngular.csproj(0,0): Error: Detected package version outside of dependency constraint: Microsoft.AspNetCore.App 2.1.1 requires Microsoft.EntityFrameworkCore.Relational (>= 2.1.1 && < 2.2.0) but version Microsoft.EntityFrameworkCore.Relational 2.2.2 was resolved.
d:\Mijn Documents\UDEMY\aspnetAngular\aspnetAngular.csproj(0,0): Error: Detected package version outside of dependency constraint: Microsoft.AspNetCore.App 2.1.1 requires Microsoft.EntityFrameworkCore.SqlServer (>= 2.1.1 && < 2.2.0) but version Microsoft.EntityFrameworkCore.SqlServer 2.2.2 was resolved.
d:\Mijn Documents\UDEMY\aspnetAngular\aspnetAngular.csproj(0,0): Error: Version conflict detected for Microsoft.Extensions.Caching.Abstractions. Install/reference Microsoft.Extensions.Caching.Abstractions 2.2.0 directly to project aspnetAngular to resolve this issue. 
 aspnetAngular -> Microsoft.EntityFrameworkCore 2.2.2 -> Microsoft.Extensions.Caching.Memory 2.2.0 -> Microsoft.Extensions.Caching.Abstractions (>= 2.2.0) 
 aspnetAngular -> Microsoft.AspNetCore.App 2.1.1 -> Microsoft.Extensions.Caching.Abstractions (>= 2.1.1 && < 2.2.0).

[fail]: OmniSharp.MSBuild.ProjectManager
        Attemped to update project that is not loaded: d:\Mijn Documents\UDEMY\aspnetAngular\aspnetAngular.csproj

我不明白。我收到此错误:

error: Package Microsoft.AspNetCore.App 2.2.2 is not compatible with netcoreapp2.1 (.NETCoreApp,Version=v2.1). Package Microsoft.AspNetCore.App 2.2.2 supports: netcoreapp2.2 (.NETCoreApp,Version=v2.2)
error: Package 'Microsoft.AspNetCore.App' is incompatible with 'all' frameworks in project 'D:\Mijn Documents\UDEMY\aspnetAngular\aspnetAngular.csproj'.

但如果我这样做:dotnet --version

我得到了正确的版权:2.2.104

奥克做到了这一点。看到:

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

  <PropertyGroup>
    <TargetFramework>netcoreapp2.2</TargetFramework>
    <TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>
    <TypeScriptToolsVersion>Latest</TypeScriptToolsVersion>
    <IsPackable>false</IsPackable>
    <SpaRoot>ClientApp\</SpaRoot>
    <DefaultItemExcludes>$(DefaultItemExcludes);$(SpaRoot)node_modules\**</DefaultItemExcludes>

    <!-- Set this to true if you enable server-side prerendering -->
    <BuildServerSideRenderer>false</BuildServerSideRenderer>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" />
    <PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="2.1.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="2.2.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Analyzers" Version="2.2.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="2.2.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.sqlServer" Version="2.2.2" />
  </ItemGroup>

  <ItemGroup>
    <!-- Don't publish the SPA source files, but do show them in the project files list -->
    <Content Remove="$(SpaRoot)**" />
    <None Include="$(SpaRoot)**" Exclude="$(SpaRoot)node_modules\**" />
  </ItemGroup>

  <Target Name="DebugEnsureNodeEnv" BeforeTargets="Build" Condition=" '$(Configuration)' == 'Debug' And !Exists('$(SpaRoot)node_modules') ">
    <!-- Ensure Node.js is installed -->
    <Exec Command="node --version" ContinueOnError="true">
      <Output TaskParameter="ExitCode" PropertyName="ErrorCode" />
    </Exec>
    <Error Condition="'$(ErrorCode)' != '0'" Text="Node.js is required to build and run this project. To continue, please install Node.js from https://nodejs.org/, and then restart your command prompt or IDE." />
    <Message Importance="high" Text="Restoring dependencies using 'npm'. This may take several minutes..." />
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm install" />
  </Target>

  <Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
    <!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm install" />
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm run build -- --prod" />
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm run build:ssr -- --prod" Condition=" '$(BuildServerSideRenderer)' == 'true' " />

    <!-- Include the newly-built files in the publish output -->
    <ItemGroup>
      <DistFiles Include="$(SpaRoot)dist\**; $(SpaRoot)dist-server\**" />
      <DistFiles Include="$(SpaRoot)node_modules\**" Condition="'$(BuildServerSideRenderer)' == 'true'" />
      <ResolvedFileToPublish Include="@(DistFiles->'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">
        <RelativePath>%(DistFiles.Identity)</RelativePath>
        <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
      </ResolvedFileToPublish>
    </ItemGroup>
  </Target>

</Project>

但是,如果我运行该项目,我仍然会收到此错误:

D:\Mijn Documents\UDEMY\aspnetAngular\aspnetAngular.csproj : error NU1605: Detected package downgrade: Microsoft.AspNetCore.SpaServices.Extensions from 2.2.0 to 2.1.1. Reference the package directly from the project to select a different version. 
D:\Mijn Documents\UDEMY\aspnetAngular\aspnetAngular.csproj : error NU1605:  aspnetAngular -> Microsoft.AspNetCore.App 2.2.0 -> Microsoft.AspNetCore.SpaServices.Extensions (>= 2.2.0 && < 2.3.0) 
D:\Mijn Documents\UDEMY\aspnetAngular\aspnetAngular.csproj : error NU1605:  aspnetAngular -> Microsoft.AspNetCore.SpaServices.Extensions
(>= 2.1.1)

所以我找到了一个“解决方案” - 你必须删除PacketReference所以我这样做了,但现在我收到了这个错误:

D:\Mijn Documents\UDEMY\aspnetAngular\aspnetAngular.csproj : error NU1605: Detected package downgrade: Microsoft.AspNetCore.Razor.Design
from 2.2.0 to 2.1.2. Reference the package directly from the project to select a different version. 
D:\Mijn Documents\UDEMY\aspnetAngular\aspnetAngular.csproj : error NU1605:  aspnetAngular -> Microsoft.AspNetCore.App 2.2.0 -> Microsoft.AspNetCore.Razor.Design (>= 2.2.0 && < 2.3.0)
c# asp.net-core command-line-interface
1个回答
1
投票

看来你正在使用EFCore版本2.2,你的项目基础dotnet版本是2.1。在这种情况下,如果你的系统上没有dotnet版本2.2,你必须将你的EFCore降级到版本2.1.*

© www.soinside.com 2019 - 2024. All rights reserved.