entity-framework-coremaui

MyApp.deps.json not found using command "dotnet ef migrations add InitialCreate"


I wanted to switch my .NET MAUI App (.net 7) from using SQLite to EF Core to use migrations to get Updates of my App handled more easily.

Therefore, I added the following NuGet-Packages in my csproj:

<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.14" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.14">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.14" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Core" Version="7.0.14" />

My csproj looks like this:

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

    <PropertyGroup>
        <TargetFrameworks>net7.0-ios;net7.0-android</TargetFrameworks>
        <OutputType>Exe</OutputType>
        <RootNamespace>MyApp</RootNamespace>
        <UseMaui>true</UseMaui>
        <SingleProject>true</SingleProject>
        <ImplicitUsings>enable</ImplicitUsings>

        <!-- Display name -->
        <ApplicationTitle>My App</ApplicationTitle>

        <!-- App Identifier -->
        <ApplicationId>com.companyname.MyApp</ApplicationId>
        <ApplicationIdGuid>42a7bc41-46c4-478-9d6a-af3479e68c63</ApplicationIdGuid>

        <!-- Versions -->
        <ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
        <ApplicationVersion>1</ApplicationVersion>

        <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">11.0</SupportedOSPlatformVersion>
        <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">21.0</SupportedOSPlatformVersion>
    </PropertyGroup>

    <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net7.0-ios|AnyCPU'">
      <MtouchLink>SdkOnly</MtouchLink>
    </PropertyGroup>

    <PropertyGroup Condition="'$(TargetFramework)'=='net7.0-ios'">
      <EnableAssemblyILStripping>false</EnableAssemblyILStripping>
      <CodesignKey>Apple Development: Created via API (AEGHV5562R)</CodesignKey>
      <CodesignProvision>VS: WildCard Development</CodesignProvision>
    </PropertyGroup>

    <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net7.0-ios|AnyCPU'">
      <MtouchLink>SdkOnly</MtouchLink>
    </PropertyGroup>

    <ItemGroup>
        <!-- App Icon -->
        <MauiIcon Include="Resources\AppIcon\appicon.svg" Color="#512BD4" />
        
        <!-- Images -->
        <MauiImage Include="Resources\Images\*" />
        <MauiImage Update="Resources\Images\dotnet_bot.svg" BaseSize="168,208" />

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

        <!-- Raw Assets (also remove the "Resources\Raw" prefix) -->
        <MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />
    </ItemGroup>
    
    <ItemGroup Condition="'$(TargetFramework)'!='net7.0-ios'">
      <MauiSplashScreen Include="Resources\Splash\mo_splash.png" Color="#000000" BaseSize="1024,1024" Resize="false" />
    </ItemGroup>

    <ItemGroup>
      <None Remove="Resources\Images\mo_splash.png" />
    </ItemGroup>
    
    <ItemGroup Condition="'$(TargetFramework)'=='net7.0-ios'">
        <InterfaceDefinition Include="LaunchScreen.storyboard" />
        <BundleResource Include="Resources\Images\mo_splash.png" />
    </ItemGroup>
    
    <ItemGroup>
        <PackageReference Include="CommunityToolkit.Diagnostics" Version="8.2.2" />
        <PackageReference Include="CommunityToolkit.Maui" Version="5.3.0" />
        <PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
        <PackageReference Include="LocalizationResourceManager.Maui" Version="1.2.1" />
        <PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.14" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.14">
            <PrivateAssets>all</PrivateAssets>
            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        </PackageReference>
        <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.14" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Core" Version="7.0.14" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.14">
          <PrivateAssets>all</PrivateAssets>
          <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        </PackageReference>
        <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="7.0.0" />
        <PackageReference Include="Refractored.MvvmHelpers" Version="1.6.2" />
        <PackageReference Include="Sharpnado.CollectionView.Maui" Version="3.0.0" />
        <PackageReference Include="Sharpnado.Tabs.Maui" Version="3.0.1" />
        <PackageReference Include="sqlite-net-pcl" Version="1.9.141-beta" />
        <PackageReference Include="SQLitePCLRaw.bundle_green" Version="2.1.7" />
        <PackageReference Include="SQLitePCLRaw.core" Version="2.1.7" />
        <PackageReference Include="SQLitePCLRaw.provider.dynamic_cdecl" Version="2.1.7" />
    </ItemGroup>

    <ItemGroup>
      <MauiXaml Update="Pages\Help.xaml">
        <Generator>MSBuild:Compile</Generator>
      </MauiXaml>
      <MauiXaml Update="Pages\MainPage.xaml">
        <Generator>MSBuild:Compile</Generator>
      </MauiXaml>
      <MauiXaml Update="Pages\ManageItem.xaml">
        <Generator>MSBuild:Compile</Generator>
      </MauiXaml>
      <MauiXaml Update="Pages\Settings.xaml">
        <Generator>MSBuild:Compile</Generator>
      </MauiXaml>
      <MauiXaml Update="Pages\Views\BackgroundImageView.xaml">
        <Generator>MSBuild:Compile</Generator>
      </MauiXaml>
      <MauiXaml Update="Pages\Views\ItemCompositionView.xaml">
        <Generator>MSBuild:Compile</Generator>
      </MauiXaml>
    </ItemGroup>

    <ItemGroup>
      <ProjectReference Include="..\..\Solver\Solver\Solver\Solver.csproj" />
      <ProjectReference Include="..\Maui.Controls.BottomSheet-main\src\Maui.Controls.BottomSheet\Maui.Controls.BottomSheet.csproj" />
      <ProjectReference Include="..\SQLiteNetExtensionsAsync\SQLiteNetExtensionsAsync.csproj" />
      <ProjectReference Include="..\SQLiteNetExtensions\SQLiteNetExtensions.csproj" />
    </ItemGroup>

    <ItemGroup>
      <Compile Update="Pages\Views\BackgroundImageView.xaml.cs">
        <DependentUpon>BackgroundImageView.xaml</DependentUpon>
      </Compile>
      <Compile Update="Resources\Localization\AppResources.Designer.cs">
        <DesignTime>True</DesignTime>
        <AutoGen>True</AutoGen>
        <DependentUpon>AppResources.resx</DependentUpon>
      </Compile>
    </ItemGroup>

    <ItemGroup>
      <EmbeddedResource Update="Resources\Localization\AppResources.resx">
        <Generator>ResXFileCodeGenerator</Generator>
        <LastGenOutput>AppResources.Designer.cs</LastGenOutput>
      </EmbeddedResource>
    </ItemGroup>

    <ItemGroup>
      <EditorConfigFiles Remove="D:\Programming\C#\MyApp\MyApp\.editorconfig" />
    </ItemGroup>

    <ItemGroup>
      <None Include="D:\Programming\C#\MyApp\MyApp\.editorconfig" />
    </ItemGroup>

</Project>

I migrated my Models to EF Core and added a DatabaseContext-File. My Models are located under "Database\Models", my DatabaseContext unter "Database".

I've built my App and restored all packages successfully.

In the "Package Manager Console" I've set my Project as Startup-Project and also within my solution it is set as the Startup-Project.

Executing the following command, I get the error below:

dotnet ef migrations add InitialCreate

Error:

Sometimes it mentions it for iOS, but also for Android

PM> dotnet ef migrations add InitialCreate
Build started...
Build succeeded.
dotnet : The specified deps.json [D:\Programming\C#\MyApp\MyApp\bin\Debug\net7.0-ios\iossimulator-x64\MyApp.deps.json] does not exist
At line:1 char:1
+ dotnet ef migrations add InitialCreate
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (The specified d... does not exist:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
 

I've also tried with a new Console-Project (.net 7) and added all my Models and DatabaseContext classes to it. There, it works without errors and is not complaining about a missing deps.json file. I can also locate the correspondig *.deps.json in my output folder.

On the internet I found hints to delete packages folder and get all of them again => did not work.

I also found a hint to set the startup project again. I did within my solution and also within Package Manager Console => did not work.

I it might work work, to copy all of my Models and Datbase Context to this separate Console-Project I created and keep the Namespaces same as in my .NET MAUI App. Then create the migrations as my database changes and copy these migrations to my .NET MAUI project. But, I am not sure about that due to the *.deps.json file from the Console-Project. It is mentioning .NET Core in here and my output is <TargetFrameworks>net7.0-ios;net7.0-android</TargetFrameworks>

{
  "runtimeTarget": {
    "name": ".NETCoreApp,Version=v7.0",
    "signature": ""
  },
  "compilationOptions": {},
  "targets": {
    ".NETCoreApp,Version=v7.0": {
      "MacroOptimizerApp/1.0.0": {
        "dependencies": {
          "Microsoft.EntityFrameworkCore": "7.0.14",
          "Microsoft.EntityFrameworkCore.Design": "7.0.14",
          "Microsoft.EntityFrameworkCore.Sqlite": "7.0.14",
          "Microsoft.EntityFrameworkCore.Sqlite.Core": "7.0.14"
        },
        "runtime": {
          "MyApp.dll": {}
        }
      },
      ...
}


So far, I have no clue why my *.deps.json is missing and what I can do to execute the dotnet ef migrations add ... command successfully. Are there any hints?


Solution

  • About this error, you can refer to my previous answer in this case: deps.json does not exist in Entity Framework Core 7 in .NET MAUI app:

    This is a known issue: The specified deps.json file doesn't exist #22247, and the comment in it said:

    There is a path bug here. The deps file is looked for in the path bin\<Platform>\<TargetFramework>\<TargetFramework>\<Runtime>\

    Note the two occurrences of TargetFramework and the absence of Configuration. My outdir is supposed to be bin\<Platform>\<Configuration>\<TargetFramework>\<Runtime> as specified in my csproj. This command needs to look for the file in the same output folder that msbuild builds to, but it is not.

    Note that I am doing this with the command line tools (dotnet ef migrations). I updated from version 5 to version 7.0.3 and still encounter this bug.

    Copying the output folder to the specified directory is a workaround, though you will have to remember to keep copying it if you need to make changes.

    And also this case: The specified deps.json [...] does not exist.