如何解决 The type 'AssemblyCompanyAttribute' exists both 'System.Runtime,' and 'netstandard'?

How to resolve The type 'AssemblyCompanyAttribute' exists in both 'System.Runtime,' and 'netstandard'?

我正在尝试将 visual studio 代码、.net core 2.2 和 entityframework 合而为一。

我执行以下步骤:

1) 创建新的 sln

dotnet new sln --name MySln

2) 创建一个 webapi:

dotnet new webapi --name api

3) 创建一个 class 库:

dotnet new classlib --name Api.DB

添加对项目的引用:

dotnet sln "MyApi.sln" add "api/api.csproj"

dotnet sln "MyApi.sln" add "Api.DB/Api.DB.csproj"

dotnet add reference ../Api.DB/Api.DB.csproj

在 classlib 中添加以下内容:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
     <RuntimeFrameworkVersion>2.0.5</RuntimeFrameworkVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.4"/>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.2.4"/>
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.4"/>
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="2.0.0-preview1-final"/> 
  </ItemGroup>
     <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.1" />
  </ItemGroup>
</Project>

问题我不明白但是我在 classlib 中收到以下错误:

The type 'AssemblyCompanyAttribute' exists in both 'System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' and 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' (CS0433) [Api.DB]

编辑:

忘记提及在 classlib 中注释我的所有包时像这样:

<!-- <PackageReference Include="Microsoft.EntityFrameworkCore" Version="*"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="*"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="*"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="*"/> -->

然后错误就消失了,这让我更加困惑了?包裹有错误吗?

当我删除它们并尝试伪造我的数据库上下文时,我得到:

Startup project 'Api.DB.csproj' targets framework '.NETStandard'. There is no runtime associated with this framework, and projects targeting it cannot be executed directly. To use the Entity Framework Core .NET Command-line Tools with this project, add an executable project targeting .NET Core or .NET Framework that references this project, and set it as the startup project using --startup-project; or, update this project to cross-target .NET Core or .NET Framework. For more information on using the EF Core Tools with .NET Standard projects, see https://go.microsoft.com/fwlink/?linkid=2034781

一段时间以来,我一直在努力解决我的错误,我终于找到了解决方案。

@Lesiak 在评论中给我 link 后,我将以下内容放入我的类库中:

  <PropertyGroup>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
  </PropertyGroup> 
  <PropertyGroup>
    <EnableDefaultRazorTargetAssemblyInfoAttributes>false</EnableDefaultRazorTargetAssemblyInfoAttributes>
  </PropertyGroup> 

这解决了原来的错误。

不过 我遵循的指南是针对 .net core 2.0 的,所以我在 classlib

中包含了以下内容
<RuntimeFrameworkVersion>2.0.5</RuntimeFrameworkVersion> 

但我的目标是 .netcore 2.2,所以我不得不将目标框架更改为:

  <TargetFrameworks>netcoreapp2.2;netstandard2.0</TargetFrameworks>