Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw a better exception when using EF Core tooling with MAUI #25938

Closed
AWildTeddyBear opened this issue Aug 27, 2021 · 9 comments · Fixed by #26094
Closed

Throw a better exception when using EF Core tooling with MAUI #25938

AWildTeddyBear opened this issue Aug 27, 2021 · 9 comments · Fixed by #26094
Assignees
Labels
area-platform area-tools closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-enhancement
Milestone

Comments

@AWildTeddyBear
Copy link

Description

Upon trying to create migrations in a MAUI + Blazor project, you are met with an error message "The specified deps.json does not exist"
MAUI seems to not generate this file unlike a normal .NET 6 project, and EntityFramworkCore depends on this file for creating or applying migrations.
This only happens when targeting mobile only on MAUI (including mac), UWP is likely unaffected.

Steps to Reproduce

  1. Open Visual Studio 2022 Enterprise with MAUI installed (as instructed here)

  2. Create a new .NET MAUI Blazor App project

  3. Unload/Delete the WinUI project (not necessary, but UWP is not needed for this project)

  4. Right click your MAUI project to Properties and change Target frameworks from net6.0-ios;net6.0-android;net6.0-maccatalyst to net6.0-android -- Target OS and Target OS version should automatically change (sometimes it's not immediate after you save, you might have to close the properties window and re-open before you see it change)

  5. Add the following packages to your project via your .csproj file (you can alternatively download the respective current preview versions of these packages manually via the package manager):

  • Microsoft.EntityFrameworkCore.Sqlite - Version: 6.0.0-*
  • Microsoft.EntityFrameworkCore.Design - Version: 6.0.0-*
  • Microsoft.EntityFrameworkCore.Tools- Version: 6.0.0-*
  1. Open the Data folder in your MAUI solution and create a simple class:
    Test.cs
using System.ComponentModel.DataAnnotations;

namespace Mauitest2.Data
{
    public class Test
    {
        [Key]
        public int ID { get; set; }
        public string Name { get; set; }
    }
}
  1. Create ApplicationDbContext.cs in your Data folder
using Microsoft.EntityFrameworkCore;

namespace Mauitest2.Data
{
    public class ApplicationDbContext : DbContext
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
        {
        }

        public DbSet<Test> Tests { get; set; }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);

            // Add entity relationship configurations
            builder.Entity<Test>().HasKey(x => x.ID);
        }
    }
}
  1. Add your database service in your Startup.cs
services.AddDbContext<ApplicationDbContext>(options =>
{
	options.UseSqlite("Data Source = Test.db");
});

It will look like the following:
Services

  1. Open the Package Manager Console in your project (View -> Other Windows -> Package Manager Console)

  2. Type "Add-Migration Initial" and press Enter

  3. Both Add-Migration and Update-Database both pass the build, but immediately fail afterwards with "The specified deps.json [project path\bin\debug\net6.0-android\project name.deps.json] does not exist" and no migration is created.

Expected Behavior

Add-Migration should create a migration based on the project that the EntityFrameworkCore database is setup on (in this case, MAUI)
Update-Database should apply an existing migration built by Add-Migration and build a SQLite database.

Actual Behavior

Both Add-Migration and Update-Database both pass the build, but immediately fail afterwards with "The specified deps.json [project path\bin\debug\net6.0-android\project name.deps.json] does not exist" and no migration is created or applied.

Basic Information

  • Version with issue: net6.0-android, Microsoft.Maui - 6.0.100-preview.5.794
  • Last known good version: .NET 6 Standard project
  • IDE: Visual Studio 2022 Enterprise Preview Version 17.0.0 Preview 3.1
  • Platform Target Frameworks:
    • iOS: N/A
    • Android: net6.0-android -- Version 30 (Android 11)
    • UWP: N/A
  • Android Support Library Version: net6.0-android -- Version 30 (Android 11)
  • Nuget Packages:
	<ItemGroup>
		<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.0-*" />
		<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.0-*" />
		<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.0-*" />
		<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="6.0.0-preview.7.21378.6" />
		<PackageReference Include="Microsoft.AspNetCore.Components.WebView.Maui" Version="6.0.100-preview.5.794" />
		<PackageReference Include="Microsoft.Maui" Version="6.0.100-preview.5.794" />
	</ItemGroup>
  • Affected Devices: All MAUI-specific projects.

Screenshots

DepsJsonError

Workaround

No current workaround known -- possible you can create the migrations in a different project in .NET 5, or .NET 6 and port them over to your MAUI project, including the SQLite database created.

@Eilon
Copy link
Member

Eilon commented Aug 30, 2021

Hi @AWildTeddyBear , thank you for the detailed bug report. Does this same issue happen with a regular .NET MAUI project (without Blazor)?

@AWildTeddyBear
Copy link
Author

I am currently working on trying to reproduce this in MAUI preview 7, taking a bit of setting up at the moment and running into issues described here: dotnet/maui#2235
On Android specific compilation below SDK version 30-31.
I will hopefully be able to test this soon. MAUI Libs are also a thing since originally making this, so I will experiment with those as well. My suspicion is how it's compiling on Android, I doubt this is an issue if you targeted the windows platform for MAUI.

@AWildTeddyBear
Copy link
Author

This issue is still reproducible on MAUI-Blazor on Preview 7.

This issue is also reproducible on a normal .NET MAUI project without Blazor (on preview 7)
image

My .csproj file:

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

	<PropertyGroup>
		<!-- iOS, Android, MacCatalyst -->
		<TargetFrameworks>net6.0-android</TargetFrameworks>
		<OutputType>Exe</OutputType>
		<UseMaui>true</UseMaui>
		<SingleProject>true</SingleProject>
		<RootNamespace>TesutoNi</RootNamespace>

		<!-- Display name -->
		<ApplicationTitle>TesutoNi</ApplicationTitle>

		<!-- App Identifier -->
		<ApplicationId>com.companyname.TesutoNi</ApplicationId>

		<!-- Versions -->
		<ApplicationVersion>1.0</ApplicationVersion>
		<AndroidVersionCode>1</AndroidVersionCode>
	</PropertyGroup>

	<ItemGroup>
		<!-- App Icon -->
		<MauiImage
			Include="Resources\appicon.svg"
			ForegroundFile="Resources\appiconfg.svg"
			IsAppIcon="true"
			Color="#512BD4" />

		<!-- Splash Screen -->
		<MauiSplashScreen Include="Resources\appiconfg.svg" Color="#512BD4" />

		<!-- Images -->
		<MauiImage Include="Resources\Images\*" />

		<!-- Custom Fonts -->
		<MauiFont Include="Resources\Fonts\*" />
	</ItemGroup>

	<PropertyGroup>
		<InvariantGlobalization Condition="$(TargetFramework.Contains('-maccatalyst'))">true</InvariantGlobalization>
		<RuntimeIdentifier Condition="$(TargetFramework.Contains('-ios'))">iossimulator-x64</RuntimeIdentifier>
		<RuntimeIdentifier Condition="$(TargetFramework.Contains('-maccatalyst'))">maccatalyst-x64</RuntimeIdentifier>
		<UseInterpreter Condition="$(TargetFramework.Contains('-android'))">False</UseInterpreter>
	</PropertyGroup>

	<ItemGroup>
		<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.0-*" />
		<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.0-*" />
		<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.0-*" />
	</ItemGroup>
</Project>

Reproduction steps were identical to blazor-maui stack, just making the class and ApplicationDbContext in the root of the project.

@marqdouj
Copy link

marqdouj commented Sep 4, 2021

yep, just tried this for the first time and I am getting the same error; including with the WinUI project.

@Eilon
Copy link
Member

Eilon commented Sep 7, 2021

I can reproduce this in the latest dev bits of .NET MAUI. Apparently this is a long-known EF Core issue, but I'm not sure what the solution is. I'm digging deeper to see what the root issue is and how we can fix.

@Eilon
Copy link
Member

Eilon commented Sep 7, 2021

Lots of matches on the EF repo, all duped around the issue I previously mentioned: https://github.com/dotnet/efcore/issues?q=is%3Aissue+The+specified+deps.json+

@Eilon
Copy link
Member

Eilon commented Sep 7, 2021

And even an issue logged on the AspNetCore repo: dotnet/aspnetcore#17751

So either way I feel like this likely isn't a .NET MAUI root cause, but something more general with VS or EF.

Also, I reproed this in a regular .NET MAUI project (no Blazor), so this isn't Blazor-specific.

@Eilon
Copy link
Member

Eilon commented Sep 8, 2021

It appears that this is currently not supported because EF needs to be able to run part of your app's code, but because your app's code targets an environment that can be run locally (Android, in this case), it isn't supposed to work. But, the error you're getting is of course not ideal.

Fortunately, there are workarounds to this. In general, you need to move your DbContext and related types to a separate class library project that targets net6.0 (not net6.0-android, etc.), and then create an additional console app project that has the appropriate startup code that EF can use for its startup project.

You can learn more in these docs:

I'm also transferring this issue to the EF Core repo so that the experience can be improved, at least in terms of detecting unsupported scenarios and offering better error messages and guidance.

@Eilon Eilon transferred this issue from dotnet/maui Sep 8, 2021
@ajcvickers ajcvickers changed the title [Bug] The specified deps.json does not exist -- MAUI + Blazor stack with EntityFrameworkCore Migrations The specified deps.json does not exist -- MAUI + Blazor stack with EntityFrameworkCore Migrations Sep 11, 2021
@ajcvickers ajcvickers added this to the 6.0.0 milestone Sep 11, 2021
@ajcvickers ajcvickers changed the title The specified deps.json does not exist -- MAUI + Blazor stack with EntityFrameworkCore Migrations Throw a better exception when using EF Core tooling with MAUI Sep 11, 2021
@ajcvickers
Copy link
Member

Notes from triage:

bricelam added a commit to bricelam/efcore that referenced this issue Sep 17, 2021
@bricelam bricelam added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Sep 17, 2021
bricelam added a commit to bricelam/efcore that referenced this issue Sep 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-platform area-tools closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants