1

I created an UWP app and I will use entityFrameworkCore to create my DB with code first. My app console (entityFrameworkCore app) works and creates my DB but when I make a reference to my UWP project it doesn't work and shows this error:

The project uwp is not compatible with netcoreapp2.1(.NETCoreApp,version=v2.1)

And when i would like add a reference in uwp to db project i have this error enter image description here

bertrand
  • 23
  • 6
  • No, it is not compatible. It will be in the future https://devblogs.microsoft.com/dotnet/net-core-3-and-support-for-windows-desktop-applications/ – mortb Apr 02 '19 at 09:23
  • Thanks @mortb how can i resolve my problem ? – bertrand Apr 02 '19 at 09:26
  • However, entity-framework-core and uwp is .netstandard compatible and thus able to run together: https://devblogs.microsoft.com/dotnet/uwp-net-standard-2-0-preview/ It is the target .netcoreapp in your project that makes it go wrong. The target says that your project should run on the .net core runtime, which (currently) can't run uwp. To get around this I think you could *first* create your uwp app project and *then* add a reference to entity-framework-core. That way your project will target uwp – mortb Apr 02 '19 at 09:36
  • @bertrand: Which version/NuGet package of EF core are you using? Why are you targeting .NET Core 2.1? Both UWP and EF Core supports .NET Standard 2.0. A .NET Core app shouldn't add a reference to a UWP app anyway. – mm8 Apr 02 '19 at 12:55
  • I use EFcore 2.2.3 and my app consol is in .NET core 2.1. i don't use a .NET standard app because i need a program.cs to create my DB – bertrand Apr 02 '19 at 13:06
  • @bertrand: So where does your UWP fit into this picture...? A console app shouldn't reference another app... – mm8 Apr 02 '19 at 14:03
  • So how can I use my dBContext in my uwp project – bertrand Apr 02 '19 at 14:05
  • @bertrand: Create a .NET Standard 2.0 project that references EF Core and defines the `DbContext`. You could then reference this project from your console app and your UWP app. See my answer. – mm8 Apr 02 '19 at 14:23

2 Answers2

0

UWP isn't supported in .net core so creating .net core app won't work. create .net standart console app and include it into your UWP project

ARandomCoder
  • 196
  • 5
  • I do this but the probleme it's with a .netstandar app there aren't program.cs and i can't launch my migration and create my db with entityFrameworkCore codeFirst – bertrand Apr 02 '19 at 09:51
  • can we talk anywhere maybe? – bertrand Apr 02 '19 at 10:59
  • oh i see. create console application with .net framework. it has program cs and you can run it normally. when you will create your database add reference in Dependencies of your UWP project. hope this will help – ARandomCoder Apr 02 '19 at 11:14
  • Now i can't add references in my twice projects ( UWP and DB). I have an error screen ''Impossible to add a reference on project "UWP"' Don't know why =/ – bertrand Apr 02 '19 at 13:00
  • Is DB project part of the solution? also you shouldn't reference UWP project in your DB project. If DB project isn't part of the solution add it first and after that you'll be able to reference it. – ARandomCoder Apr 02 '19 at 13:06
  • DB project is in the same solution that the uwp project and i just would like use my DbContext in my UWP project and so i must add a reference of uwp in db project (if I'm not mistaken) – bertrand Apr 02 '19 at 13:14
  • no it's other way around. you should add DB project reference into UWP project. main function will run from UWP project so main thread is in UWP project. – ARandomCoder Apr 02 '19 at 14:12
0

A .NET Core app cannot add a reference to a UWP app and vice versa.

What you should do is to create a .NET Standard project where you install Entity Framework Core and define your DbContext.

You could then reference this project from both the .NET Core console app and from the UWP app.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • I try it, is it the same way that the "Microsoft get started" and when i create my initial migration i have this error > No DbContext was found in assembly 'Migration'. Ensure that you're using the correct assembly and that the type is neither abstract nor generic. – bertrand Apr 03 '19 at 08:05
  • I try to create a UWP project with MvvmLight and EFCore (code first) Maybe we can tchat ? – bertrand Apr 03 '19 at 08:09
  • Did you add a reference to 'Migration' from the Uwp app? Did you actually define a public `DbContext` class in there? – mm8 Apr 03 '19 at 08:41
  • there a reference to "Migration" and "UWP" in Db project . Yes my context is in public – bertrand Apr 03 '19 at 09:05
  • @bertrand: There should *not* be a reference to the UWP app from any project. The UWP app should reference the projects. – mm8 Apr 03 '19 at 09:05
  • Yes you 're right. it's like this => In "Migration" there are a reference to Db and in "UWP" there are a reference to Db. – bertrand Apr 03 '19 at 09:12
  • @bertrand: What's in migration? Shouldn't you reference this project from the UWP app instead of Db? – mm8 Apr 03 '19 at 09:14
  • Migration is a .NET Core console app. In migration i have a program.cs to create my Db and apply migration. certainly i should add a reference to"Migration" in "UWP" but the problem is in uphill. My db do not created – bertrand Apr 03 '19 at 09:20
  • You should read my answer once more...you need to move the code that creates the DbContext to a .NET Standard project. – mm8 Apr 03 '19 at 09:25
  • So how execute my migration if there not main method ? In my program.cs i have "var context = Initialize.GetContext();" and with this my db is created and my migration applyed – bertrand Apr 03 '19 at 09:30
  • Do you really need to perform the migration each time the app is started? Then should do it in the UWP app I guess. – mm8 Apr 03 '19 at 09:33
  • If i recap, i create 2 project "UWP" and "Db". The Db project (.NET Standard) create my Db with code first based on my model and in "UWP" i reference "Db" project and i can write this "using(var db= new DbContext()" – bertrand Apr 03 '19 at 09:38
  • @bertrand: So what's your current issue? – mm8 Apr 03 '19 at 09:55
  • Now i have an SqlClient exception he didn't find my Server – bertrand Apr 03 '19 at 11:39
  • Please ask a new question if you have another issue. You need to move the configuration or whatever you use to connect to the db over the UWP project. – mm8 Apr 03 '19 at 11:43