【问题标题】:How to specify the targetframework for nuget dependencies?如何为 nuget 依赖项指定目标框架?
【发布时间】:2018-11-01 06:48:00
【问题描述】:

我使用 targetframework Netstandard 2.0 创建了一个私有 nuget 包 该包依赖于 EntityFrameworkCore 和 EntityFramework.DbContextScope。

如果我在 .Net Core 项目中使用它,但在 .net Framework 4.6.1 项目中使用它,这非常好。

EntityFramework.DbContextScope 包导致了这些问题。我查看了 nuget 中的源代码并在 project.json 中看到了这个

"frameworks": {
"netstandard1.3": {
  "imports": [

  ],
  "dependencies": {
    "NETStandard.Library": "1.6.0",
    "Microsoft.EntityFrameworkCore.Relational": "1.0.0"
  },
  "buildOptions": {
    "define": [
      "EFCore"
    ]

  }
},
"net46": {
  "dependencies": {
    "EntityFramework": "6.1.3"
  },
  "buildOptions": {
    "define": [
      "EF6"
    ]
  },
  "frameworkAssemblies": {
    "System.Data": "4.0.0.0"
  }
},

如果我在 .net framework 4.6.1 项目中安装我的 nuget 包,它也会自动安装 EntityFramework 6,但我使用 EntityFrameworkCore 创建了我的 Nuget 包。

我能否对我的 Nuget 包进行更改,使其始终使用 targetframework netstandard 而不是 net46 安装 EntityFramework.DbContextScope?

编辑:到目前为止,我能完成这项工作的唯一方法是手动编辑 .net framework 4.6.1 项目的 csproj 文件中的提示路径。这可行,但远非理想。

 <Reference Include="EntityFramework.DbContextScope, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
  <HintPath>..\packages\EntityFramework.DbContextScope.1.0.0\lib\netstandard1.3\EntityFramework.DbContextScope.dll</HintPath>
</Reference>

【问题讨论】:

    标签: .net .net-core nuget


    【解决方案1】:

    这个question 可能会有所帮助,因为它展示了如何根据目标框架指定依赖项。

    在你的情况下,它可能看起来像:

    <dependencies>
       <group targetFramework="v4.6.1">
          <dependency id="NETStandard.Library" version="1.6.0" />
          <dependency id="Microsoft.EntityFrameworkCore.Relational" version="1.0.0" />
       </group>
    </dependencies>
    

    如果您希望它始终安装这些版本的依赖项而不考虑目标框架,您也可以不指定目标框架:

    <dependencies>
       <group>
          <dependency id="NETStandard.Library" version="1.6.0" />
          <dependency id="Microsoft.EntityFrameworkCore.Relational" version="1.0.0" />
       </group>
    </dependencies>
    

    【讨论】:

      猜你喜欢
      • 2012-02-27
      • 1970-01-01
      • 2019-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-15
      • 1970-01-01
      相关资源
      最近更新 更多