Skip to content

EF migration generation on publish fails with artifacts output layout (.net 8) #37422

@ArturDorochowicz

Description

@ArturDorochowicz

Describe the bug

EFCore migration script generation on publish is broken when artifacts output layout is enabled.

This is with .Net SDK 8.0.100.

To Reproduce

Set up the project

dotnet new tool-manifest
dotnet tool install dotnet-ef --version 8.0.0
dotnet new web
dotnet add package Microsoft.EntityFrameworkCore.Design --version 8.0.0
dotnet add package Microsoft.EntityFrameworkCore.SqlServer --version 8.0.0
dotnet new buildprops --use-artifacts

Add MyDbContext.cs with

using Microsoft.EntityFrameworkCore;
namespace MyNs;

public class MyDbContext : DbContext
{
	public DbSet<Item> Items { get; set; }

	protected override void OnConfiguring(DbContextOptionsBuilder options)
		=> options.UseSqlServer();
}

public class Item
{
	public int Id { get; set; }
	public string? Name { get; set; }
}

Change the .csproj to include:

<ItemGroup>
  <EFMigrations Include="MyNs.MyDbContext" />
</ItemGroup>

Finally add a migration:

dotnet ef migrations add InitialCreate

Now we can try to publish the project. This fails because the invocation of dotnet ef has not been updated for artifacts output layout.

> dotnet publish
MSBuild version 17.8.3+195e7f5a3 for .NET
  Determining projects to restore...
  All projects are up-to-date for restore.
  src -> /src/artifacts/bin/src/release/src.dll
  src -> /src/artifacts/publish/src/release/
  Generating Entity Framework SQL Scripts...
  Executing command: dotnet ef migrations script --no-build --idempotent --configuration Release --output "/src/artifacts/obj/src/Release/net8.0/PubTmp/EFSQLScripts/MyNs.MyDbContext.sql" --context MyNs.MyDbContext 
  /src/src.csproj : error MSB4057: The target "GetEFProjectMetadata" does not exist in the project.
  Unable to retrieve project metadata. Ensure it's an SDK-style project. If you're using a custom BaseIntermediateOutputPath or MSBuildProjectExtensionsPath values, Use the --msbuildprojectextensionspath option.
  
/usr/share/dotnet/sdk/8.0.100/Sdks/Microsoft.NET.Sdk.Publish/targets/TransformTargets/Microsoft.NET.Sdk.Publish.TransformFiles.targets(221,5): error : Entity Framework SQL Script generation failed [/src/src.csproj]

Exceptions (if any)

Further technical details

  • Include the output of dotnet --info
  • The IDE (VS / VS Code/ VS4Mac) you're running on, and its version
> dotnet --info
.NET SDK:
 Version:           8.0.100
 Commit:            57efcf1350
 Workload version:  8.0.100-manifests.6c33ef20

Runtime Environment:
 OS Name:     debian
 OS Version:  12
 OS Platform: Linux
 RID:         linux-x64
 Base Path:   /usr/share/dotnet/sdk/8.0.100/

.NET workloads installed:
 Workload version: 8.0.100-manifests.6c33ef20
There are no installed workloads to display.

Host:
  Version:      8.0.0
  Architecture: x64
  Commit:       5535e31a71

.NET SDKs installed:
  8.0.100 [/usr/share/dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 8.0.0 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 8.0.0 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

Other architectures found:
  None

Environment variables:
  Not set

global.json file:
  Not found

Learn more:
  https://aka.ms/dotnet/info

Download .NET:
  https://aka.ms/dotnet/download

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions