Reputation: 1
I can seem to be able to create migrations using this command:
dotnet ef migrations add InitialCreate --output-dir data\Migrations
This is the error I get:
The Entity Framework tools version '8.0.10' is older than that of the runtime '9.0.0-rc.2.24474.1'. Update the tools for the latest features and bug fixes. See https://aka.ms/AAc1fbw for more information.
Unable to create a 'DbContext' of type 'RuntimeType'. The exception 'Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions`1[GameStore.data.GameStoreContext]' while attempting to activate 'GameStore.data.GameStoreContext'.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
My .csproj
file:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.0-rc.2.24474.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.0-rc.2.24474.1" />
<PackageReference Include="MinimalApis.Extensions" Version="0.11.0" />
</ItemGroup>
</Project>
GameStoreContext.cs
:
using System;
using GameStore.Entities;
using Microsoft.EntityFrameworkCore;
namespace GameStore.data
{
public class GameStoreContext(DbContextOptions<GameStoreContext> options) : DbContext(options)
{
// DbSets representing tables in your database
public DbSet<Game> Games { get; set; }
public DbSet<Genre> Genres { get; set; }
}
}
appsettings.json
:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"GameStore": "Data Source=GameStore.db"
}
}
SQLite3 is not been recognised in the VS Code integrated terminal, but works fine in the cmd
Upvotes: 0
Views: 585
Reputation: 131641
This is caused because the .NET 9 RC1 SDK is installed on the machine and therefore the tooling runs on .NET 9 RC1. When you run dotnet build
or dotnet publish
you get a warning that you're running on a preview .NET SDK version.
The robust solution is to create a global.json file at the solution's root, specifying which .NET SDK version to use. This can be done with dotnet new globaljson
This would prevent using Preview and RC versions without hard-coding the SDK version
{
"sdk": {
"allowPrerelease": false
}
}
while this uses a specific version or any newer feature version that matches the major and minor, eg 8.0.303
, 8.0.304
etc.
{
"sdk": {
"version": "8.0.302",
"rollForward": "latestFeature"
}
}
The adventurous solution is to upgrade the EF Tools to the latest version with
dotnet tool update --global --prerelease dotnet-ef
Keep in mind that .NET 9 is at RC2 now, so the ET Tools prerelease version is 9.0.0-rc.2.24474.1
To get the RC1 version you'll have to specify it explicitly
dotnet tool update --global --prerelease --version 9.0.0-rc.1.24451.1 dotnet-ef
Upvotes: 1