【问题标题】:How to resolve The type 'AssemblyCompanyAttribute' exists in both 'System.Runtime,' and 'netstandard'?如何解决“System.Runtime”和“netstandard”中都存在“AssemblyCompanyAttribute”类型?
【发布时间】:2019-05-08 05:24:58
【问题描述】:

我正在尝试将 Visual Studio 代码、.net core 2.2 和 entityframework 合二为一。

我执行以下步骤:

1) 创建新的 sln

dotnet new sln --name MySln

2)创建一个webapi:

dotnet new webapi --name api

3) 创建类库:

dotnet new classlib --name Api.DB

添加对项目的引用:

dotnet sln "MyApi.sln" 添加 "api/api.csproj"

dotnet sln "MyApi.sln" 添加 "Api.DB/Api.DB.csproj"

dotnet 添加参考 ../Api.DB/Api.DB.csproj

在类库中我添加以下内容:

<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>

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

“AssemblyCompanyAttribute”类型存在于“System.Runtime”中, 版本=4.1.0.0,文化=中性,PublicKeyToken=b03f5f7f11d50a3a' 和 'netstandard,版本=2.0.0.0,文化=中性, PublicKeyToken=cc7b13ffcd2ddd51' (CS0433) [Api.DB]

编辑:

忘了提一下,当我像这样在类库中注释掉我所有的包时:

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

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

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

启动项目“Api.DB.csproj”以框架“.NETStandard”为目标。 没有与此框架关联的运行时,并且项目 定位它不能直接执行。使用实体框架 使用此项目的核心 .NET 命令行工具,添加可执行文件 针对引用此的 .NET Core 或 .NET Framework 的项目 项目,并使用--startup-project将其设置为启动项目; 或者,将此项目更新为跨目标 .NET Core 或 .NET Framework。 有关使用 .NET Standard 的 EF Core 工具的更多信息 项目,见https://go.microsoft.com/fwlink/?linkid=2034781

【问题讨论】:

标签: c# entity-framework asp.net-core visual-studio-code ef-database-first


【解决方案1】:

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

@Lesiak 给了我 cmets 中的链接后,我将以下内容放入我的类库中:

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

这解决了原来的错误。

然而 我为此遵循的指南适用于 .net core 2.0,因此我在类库中包含以下内容

<RuntimeFrameworkVersion>2.0.5</RuntimeFrameworkVersion> 

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-26
    • 2020-05-07
    • 2022-01-23
    • 2011-03-21
    • 1970-01-01
    相关资源
    最近更新 更多