Abdul Ahad
Abdul Ahad

Reputation: 61

Entity Framework Tools not working with UWP apps C#

Startup project 'EFGetStartedUWP' is a Universal Windows Platform app. This version of the Entity Framework Core Package Manager Console Tools doesn't support this type of project. For more information on using the EF Core Tools with UWP projects, see https://go.microsoft.com/fwlink/?linkid=858496

I'm trying to connect SQLite Database to a basic UWP app for the sake of learning but when I try migration. It just keep giving me the above error. I've searched quite a lot on the internet but didn't get the appropriate answer. The Microsoft documentation is of no use in this scenario. Also I've installed Microsoft.EntityFrameworkCore.Sqlite & Microsoft.EntityFrameworkCore.Tools.

If anyone can suggest any other way to connect SQLite database to UWP that'll be very useful too as I'm quite new to C# and I've a university project to do on a UWP app.

Upvotes: 3

Views: 2330

Answers (2)

Jess Rod
Jess Rod

Reputation: 280

I've just stepped on a similar issue, and I've been lucky enough to solve it. To start with, I found these links very useful: Entity Framework Core tools reference - .NET Core CLI - Other target frameworks and Common options.

In my case, my solution ended up with a configuration like this:

  • the application project (Universal Windows) - startup project for the solution,
  • a class library project (.NET Standard 2.0) - with the models and context classes,
  • a dummy console app project (.NET 6.0) - necessary to act like a startup project for the tools.

Have into account that (at the time of writing) UWP doesn't support .NET Standard 2.1. It supports .NET Standard 2.0 from version 10.0.16299. This means the (Universal Windows) application project can't reference a .NET Standard 2.1 project (e.g. .NET 5.0, .NET Core 3.0, Mono 6.4, etc.), so the class library project CAN'T be .NET 2.1, .NET 5.0, etc. The opposite is not true: a .NET 5.0 or .NET 6.0 project can reference a .NET Standard 2.0 project, so the dummy console app project can be .NET 6.0.

Steps to add a migration:

  1. In the class library project, install the Nuget package for the corresponding EFCore platform used (e.g. for SQLite, install Microsoft.EntityFrameworkCore.Sqlite). Do not install the most recent version but the last that depends on .NET Standard 2.0, which is 3.1.21 at the time of writing.
  2. In the dummy console app project, install this Nuget package: Microsoft.EntityFrameworkCore.Design. For compatibility purposes, install the same version as the version chosen in the previous step (e.g. 3.1.21).
  3. In the application project, add a reference to the class library project. (Right-click on the project, select Add > Project Reference..., and tick the class library project.)
  4. In the dummy console app project, add a reference to the class library project. Note there's no need to change anything else in the dummy console app project, although the project doesn't have to be built (less deployed), so feel free to open the Configuration Manager, and untick Build (and Deploy). In addition, in the Configuration Manager, don't bother to change the platform (x64, Any CPU...) to be the same as the referenced class library project. After all, the dummy console app project won't be built, so ignore any warnings about the architecture not being the same as the class library project.
  5. Rebuild your solution to make sure there are no other issues going on. If there are any, fix them before continuing.
  6. Open the command prompt (tip: navigate to the root folder of your solution with Windows File Explorer and type cmd in the address bar to open the command prompt in that folder).
  7. Type the following (-p signals the target project while -s indicates the startup project):

?> dotnet ef migrations add #MigrationName -s #DummyConsoleAppProject -p #ClassLibraryProject

  1. Optionally, unload the dummy console app project, and load it again next time you need it to add a migration.

Upvotes: 5

dydx
dydx

Reputation: 187

This looks to be a known issue from here: https://github.com/aspnet/EntityFrameworkCore/issues/9666

Using EFCore with UWP is a bit finicky.

So, add a new project to your solution -> select .net core console app -> create DBContext and your models there -> run the "Add-Migration" command and reference it back to your main project.

That should get rid of that error and let you use migrations with UWP.

Upvotes: 2

Related Questions