Efcore: Tools: Better cross-targeting support

Created on 6 Feb 2018  路  11Comments  路  Source: dotnet/efcore

When I make a UWP app, and a .NET Standard class library with Entity Framework Core, I should use cross-target like this:
<TargetFrameworks>netcoreapp2.0;netstandard2.0</TargetFrameworks>

Although, when I put the 'netstandard2.0' first, my EF will not add a new migration. And if I put the 'netcoreapp2.0' first, Visual Studio will not be able to reference this class library from my UWP project.

Steps to reproduce

  1. Create a new empty UWP project, targeting Fall Creators Update.
    image
    image

  2. In the solution, create a new project: .NET Standard class library.
    image

  3. Add a reference from the class library to the UWP project.
    image

  4. Add EF Nuget packages to the class library.
    image

  5. Make a model and DbContext.
    ```c#
    using System.ComponentModel.DataAnnotations;

namespace UWP_EF_TEST.Data
{
public class Item
{
[Key]
public int Id { get; set; }

    [Required]
    public string Value { get; set; }
}

}

```c#
using Microsoft.EntityFrameworkCore;

namespace UWP_EF_TEST.Data
{
    public class MyDbContext : DbContext
    {
        public DbSet<Item> Items { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlite("test.db");
        }
    }
}

image

  1. Try to run Add-Migration Test in the PMC, after setting the startup project to your class library. This will fail.
    image
    Error:
    Startup project 'UWP_EF_TEST.Data' 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 Package Manager Console Tools with this project, add an executable project targeting .NET Framework or .NET Core that references this project, and set it as the startup project; or, update this project to cross-target .NET Framework or .NET Core.

  2. Setup cross-target frameworks in the .csproj of your class library. Also make sure to generate migrations by adding another extra line.
    image

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

  <PropertyGroup>
    <TargetFrameworks>netstandard2.0;netcoreapp2.0</TargetFrameworks>
    <GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.0.1" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.1" />
  </ItemGroup>

</Project>
  1. Try Add-Migration Test in the PMC again. It will fail again, with the exact same message.
    image

  2. Switch the TargetFrameworks.
    image

  3. Third try to run the Add-Migration Test.
    image

  4. Cool, it worked! Let's close Visual Studio, and restart it. Now it suddenly can't add the reference anymore... Everything works fine (I can run whatever app I make), but the error won't go away. Even if I remove the reference, it's not possible to add it again. I have to switch the netcoreapp2.0 and netstandard2.0 again to make the reference. So the multiple target kind of gets ignored.
    image


Please note, I'm not sure if this is an issue for EFCore, or Visual Studio, or some other project.

EDIT: here is the repo for repro: https://github.com/BenjaVR/UWP_EF_Test

Further technical details

EF Core version: 2.0.1
Database Provider: Microsoft.EntityFrameworkCore.Sqlite
Operating system: Windows 10 Pro, version 1709, build 16299.192
IDE: Visual Studio 2017 15.4

type-enhancement

Most helpful comment

Many people have created a dummy .NET Core console app to use with the tools. Specify -StartupProject TheConsoleApp.

All 11 comments

Correct. The Visual Studio APIs return values for the first framework listed. I'm unaware of a way to specify which one to use.

Hmm, and do you know of any way to use UWP + EF without any hassle and errors etc?

We could follow up with dotnet/project-system.

The dotnet ef command has the same behavior; we could follow up with dotnet/sdk for possible solutions there.

Many people have created a dummy .NET Core console app to use with the tools. Specify -StartupProject TheConsoleApp.

Will try that one, thanks!

Do you want me to post this issue in those repositories?

9666 is about making the UWP experience better

Do you want me to post this issue in those repositories?

No, we'll follow up. I have a feeling we can better articulate exactly what we need. 馃槈

Sure, no doubt about it, thanks!

Notes

In dotnet ef, we could probably use the GetNearestTargetFramework task to be smarter.

We followed up with the project system team. (@davkean) They have no plans to change the behavior of the Visual Studio APIs (EnvDTE), nor to add first-class UI for changing the active framework.

Resolving this will require us to use lower level APIs to understand cross-targeting projects.

A first step would be to match dotnet ef and add a -Framework parameter.

After that, we could look into making dotnet ef and the PMC tools smarter about which framework it picks by default.

Putting this on the backlog now, to reconsider based on feedback. Also, updated the docs to not recommend cross-targeting.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

econner20 picture econner20  路  97Comments

bricelam picture bricelam  路  74Comments

abdu292 picture abdu292  路  99Comments

vijayantkatyal picture vijayantkatyal  路  321Comments

anpete picture anpete  路  100Comments