简体   繁体   English

多目标 .net 核心 2.2 与 .net 4.6.1

[英]Multi-targeting .net core 2.2 with .net 4.6.1

I had started on an application on gathering data from meteorological stations for some time now and to make it cross platform I used .net core 2.2 mvc along with some libraries.我已经开始使用从气象站收集数据的应用程序一段时间了,为了使其跨平台,我使用了 .net 核心 2.2 mvc 和一些库。 Even though the data is saved in a mysql db, the client recently requested that selected meteo records from a list, to be saved in a local access db.即使数据保存在 mysql 数据库中,客户端最近请求从列表中选择的气象记录保存在本地访问数据库中。 To achieve that I had to use the legit database provider from microsoft which in this case is EntityFrameworkCore.Jet .为了实现这一点,我必须使用来自微软的合法数据库提供程序,在这种情况下是EntityFrameworkCore.Jet I didn't know that there was a compatibility issue at first resulting in a起初我不知道存在兼容性问题导致

Could not load type 'System.Data.OleDb.OleDbConnection' from assembly 'System.Data,

in this part of the code:在这部分代码中:

var msAccessDb = _msAccessRepo.InitializeDatabaseContext(connectionString);
msAccessDb.Database.OpenConnection();

Now it comes to the point where I have to do some multi-targeting on my.csproj files.现在到了我必须对 my.csproj 文件进行一些多目标处理的地步。 What kind of syntax do I have to write?我必须写什么样的语法? The first thing that I have to do is update the.csproj file like so:我要做的第一件事是像这样更新 .csproj 文件:

<TargetFrameworks>netcoreapp2.2;net461</TargetFrameworks>

But all my packages like automapper, Pomelo.EntityFrameworkCore.MySql, Entity Framework tools, NLog, compile under the netcoreapp2.2 framework.但是我所有的包,比如 automapper、Pomelo.EntityFrameworkCore.MySql、Entity Framework tools、NLog,都是在 netcoreapp2.2 框架下编译的。 I get all sorts of errors mainly CS0234, and CS234 even with the Condition inside the Itemgroup xml part like so:即使项目组 xml 部分中的条件如下所示,我也会收到各种错误,主要是 CS0234 和 CS234:

  <PropertyGroup>
    <TargetFrameworks>netcoreapp2.2;net461</TargetFrameworks>    
    <AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
  </PropertyGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.2'">
    <PackageReference Include="AutoMapper" Version="9.0.0" />
    <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="7.0.0" />
    <PackageReference Include="EntityFrameworkCore.Jet" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.2.6" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="2.2.6" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.2.6">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.3" />
    <PackageReference Include="MySql.Data" Version="8.0.17" />
    <PackageReference Include="MySql.Data.EntityFrameworkCore" Version="8.0.17" />
    <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="2.2.0" />
    <PackageReference Include="System.Drawing.Common" Version="4.6.0" />
</ItemGroup>

Must I downgrade my app and change from .net core to .net framework?我必须降级我的应用程序并将 .net 核心更改为 .net 框架吗? Or am I doing something wrong in my multi-targeting?还是我在多目标中做错了什么?

Thank you in advance for your help.预先感谢您的帮助。

[ EDIT ] [编辑]

Based on the answer below, my Project.Web.csproj file is the following:根据下面的答案,我的 Project.Web.csproj 文件如下:

   <PropertyGroup>
    <TargetFrameworks>netcoreapp2.2;net461</TargetFrameworks>
    <UserSecretsId>aspnet-myProject.Web-BBF62A3C-EE9E-4808-8924-F2BEA33CDBD6</UserSecretsId>
    <AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="System.Drawing.Common" Version="4.6.0" />
  </ItemGroup>

  <ItemGroup  Condition=" '$(TargetFramework)' == 'net461'">
    <PackageReference Include="EntityFrameworkCore.Jet" Version="2.2.0" />
    <PackageReference Include="System.Data.OleDb" Version="4.6.0" />
  </ItemGroup>

  <ItemGroup  Condition=" '$(TargetFramework)' == 'netcoreapp2.2'">
    <PackageReference Include="Microsoft.AspNetCore.App" Version="2.2.0"/>   
    <PackageReference Include="Microsoft.AspNetCore" Version="2.2.0"/>    
    <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.2.6" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.2.6">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
    <PackageReference Include="AutoMapper" Version="9.0.0" />
    <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="7.0.0" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.3" />
    <PackageReference Include="MySql.Data" Version="8.0.17" />
    <PackageReference Include="MySql.Data.EntityFrameworkCore" Version="8.0.17" />
    <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="2.2.0" />
  </ItemGroup>

  <ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.2'">
    <ProjectReference Include="..\myProject.DB\myProject.DB.csproj" />
    <ProjectReference Include="..\myProject.Lib\myProject.Lib.csproj" />
    <ProjectReference Include="..\myProject.LoggerService\myProject.LoggerService.csproj" />
  </ItemGroup>

Of course it started giving me build errors because I had to apply conditions for the different frameworks they apply.当然,它开始给我构建错误,因为我必须为它们应用的不同框架应用条件。 Not that the separate libraries at the bottom, are .net core 2.2 projects only and they do not target any other framework.并不是说底部的单独库只是 .net 核心 2.2 项目,它们不针对任何其他框架。 Originally I had removed the 'Condition' but the build errors such as the following persist (as many as 850 errors).最初我已经删除了“条件”,但诸如以下的构建错误仍然存在(多达 850 个错误)。

The type or namespace name 'UserManager<>' could not be found

在此处输入图像描述

I know that I understood something wrong, or maybe the .net core 2.2 is not supported to be multi-target with .net framework as old as 4.6.我知道我理解错了,或者 .net 核心 2.2 不支持多目标,.net 框架早于 4.6。

Any further discussion to solve this, or prove that this multi-targeting will not work, wiil be appreciated.任何进一步的讨论来解决这个问题,或者证明这种多目标是行不通的,将不胜感激。

You only have package references present for .NET Core.您只有 .NET 核心的 package 参考。 You should have an item group for each of the following if they apply:如果它们适用,您应该为以下每一项设置一个项目组:

  • .NET Framework assembly references (with a target framework Condition ) .NET 框架组装参考(带有目标框架Condition
  • Nuget package references which are required for both target frameworks Nuget package 参考,这两个目标框架都需要
  • Nuget package references which are needed for .NET Framework only (with a target framework Condition )仅 .NET 框架所需的 Nuget package 参考(具有目标框架Condition
  • Nuget package references which are needed for .NET Core only (with a target framework Condition )仅 .NET 核心所需的 Nuget package 参考(具有目标框架Condition

Additionally, System.Data.OleDb.OleDbConnection is not available natively in .NET Core .此外, System.Data.OleDb.OleDbConnection在 .NET Core 中本机不可用 You will need to reference the System.Data.OleDb Nuget package.您需要参考System.Data.OleDb Nuget package。

Please try removing Condition=" '$(TargetFramework)' == 'netcoreapp2.2'" from the ItemGroup and add a reference to the package System.Data.OleDb.请尝试从ItemGroup中删除Condition=" '$(TargetFramework)' == 'netcoreapp2.2'"并添加对 package System.Data.OleDb 的引用。

If you need to take any references which are unique to a framework study my example below.如果您需要获取框架所独有的任何参考资料,请研究下面的示例。


Here, for illustration, are the package references from a library of mine which targets .NET Standard 2.0 and .NET 4.8:这里,为了说明,是来自我的库的 package 参考,它针对 .NET 标准 2.0 和 .NET 4.8:

  <ItemGroup Condition="'$(TargetFramework)'=='net48'">
    <Reference Include="System.Configuration" />
    <Reference Include="System.Data.Linq" />
    <Reference Include="System.ServiceModel" />
    <Reference Include="System.ServiceModel.Web" />
    <Reference Include="System.Web.Extensions" />
  </ItemGroup>

  <ItemGroup Condition="'$(TargetFramework)'=='netstandard2.0'">
    <PackageReference Include="System.Diagnostics.EventLog" Version="4.6.0" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="JetBrains.Annotations" Version="2019.1.3" />
    <PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
    <PackageReference Include="NodaTime" Version="2.4.7" />
  </ItemGroup>

Note how I reference .NET Framework assemblies for framework net48 , one Nuget package for netstandard2.0 , and 3 Nuget packages for both targets. Note how I reference .NET Framework assemblies for framework net48 , one Nuget package for netstandard2.0 , and 3 Nuget packages for both targets.

I have yet to understand how multi-targeting in one project works, so what worked for me, was to create the specific (connection to access db) feature in a separate webapi project targeted in .net 4.8.我还没有理解一个项目中的多目标是如何工作的,所以对我有用的是在 .net 4.8 中的一个单独的 webapi 项目中创建特定的(访问数据库的连接)功能。 The controller calls the web api method internally via httpClient and does the job. controller 通过 httpClient 在内部调用 web api 方法并完成工作。 I know that it is not ideal but for the time constraints that we have, it is a good solution.我知道这并不理想,但对于我们所拥有的时间限制,这是一个很好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM