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

.Net SDK 6.0 RC2: dotnet publish --runtime fails when project has EFMigrations #22357

Open
ArturDorochowicz opened this issue Oct 27, 2021 · 0 comments
Assignees
Labels
Area-NetSDK untriaged Request triage from a team member

Comments

@ArturDorochowicz
Copy link

ArturDorochowicz commented Oct 27, 2021

Describe the bug

If a project has EFMigrations items, dotnet publish --runtime [rid] fails:

  Generating Entity Framework SQL Scripts...
  Executing command: dotnet ef migrations script --no-build --idempotent --configuration Release --output [...] --context EFGetStarted.BloggingContext 
  
/usr/share/dotnet/sdk/6.0.100-rc.2.21505.57/Sdks/Microsoft.NET.Sdk.Publish/targets/TransformTargets/Microsoft.NET.Sdk.Publish.TransformFiles.targets(221,5):
 error : Entity Framework SQL Script generation failed

To Reproduce

Prepare the project first:

dotnet new globaljson --sdk-version 6.0.100-rc.2
dotnet new tool-manifest
dotnet tool install --local dotnet-ef --version 6.0.0-rc.2.21480.5
dotnet new web
dotnet add package Microsoft.EntityFrameworkCore.Design --version 6.0.0-rc.2.21480.5
dotnet add package Microsoft.EntityFrameworkCore.SqlServer --version 6.0.0-rc.2.21480.5

Create Model.cs with:

using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;

#nullable disable

namespace EFGetStarted
{
  public class BloggingContext : DbContext
  {
      public DbSet<Blog> Blogs { get; set; }
      public DbSet<Post> Posts { get; set; }

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

  public class Blog
  {
      public int BlogId { get; set; }
      public string Url { get; set; }

      public List<Post> Posts { get; } = new List<Post>();
  }

  public class Post
  {
      public int PostId { get; set; }
      public string Title { get; set; }
      public string Content { get; set; } 

      public int BlogId { get; set; }
      public Blog Blog { get; set; }
  }
}

Then create a migration:

dotnet ef migrations add InitialCreate

And finally change the project file (csproj) to include:

<ItemGroup>
  <EFMigrations Include="EFGetStarted.BloggingContext" />
</ItemGroup>

Now we can demonstrate the issue by running dotnet publish:

dotnet publish --runtime linux-x64 --no-self-contained
Microsoft (R) Build Engine version 17.0.0-preview-21501-01+bbcce1dff for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

  Determining projects to restore...
  Restored /src/src.csproj (in 338 ms).
  You are using a preview version of .NET. See: https://aka.ms/dotnet-core-preview
  src -> /src/bin/Debug/net6.0/linux-x64/src.dll
  src -> /src/bin/Debug/net6.0/linux-x64/publish/
  Generating Entity Framework SQL Scripts...
  Executing command: dotnet ef migrations script --no-build --idempotent --configuration Debug --output "/src/obj/Debug/net6.0/linux-x64/PubTmp/EFSQLScripts/EFGetStarted.BloggingContext.sql" --context EFGetStarted.BloggingContext 
  
/usr/share/dotnet/sdk/6.0.100-rc.2.21505.57/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]

The RID we choose doesn't matter.

It fails, because the command run internally, dotnet ef migrations script --no-build ... tries to execute the binaries of no runtime identifier and they don't exist, and it also cannot build them on its own due to --no-build.

Sadly and ironically, this bug exists due to my own suggestion in #12465. I'm not sure, but I think it has existed since SDK 5.0. I haven't spotted it earlier, because I have not used SDK 5 in this scenario.

When you try to fix this, you may consider flowing the runtime identifier into dotnet ef migrations script. This is not going to work in "cross platform" builds. By this I mean a situation where we are on e.g. Linux, but we publish for win-x64. dotnet ef must run on the current platform (Linux in this example), executing it with Windows binaries obviously won't work.

The best fix may be to remove --no-build from the command.

Exceptions (if any)

Further technical details

dotnet --info
.NET SDK (reflecting any global.json):
 Version:   6.0.100-rc.2.21505.57
 Commit:    ab39070116

Runtime Environment:
 OS Name:     debian
 OS Version:  11
 OS Platform: Linux
 RID:         debian.11-x64
 Base Path:   /usr/share/dotnet/sdk/6.0.100-rc.2.21505.57/

Host (useful for support):
  Version: 6.0.0-rc.2.21480.5
  Commit:  6b11d64e7e

.NET SDKs installed:
  6.0.100-rc.2.21505.57 [/usr/share/dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 6.0.0-rc.2.21480.10 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 6.0.0-rc.2.21480.5 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

To install additional .NET runtimes or SDKs:
  https://aka.ms/dotnet-download
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-NetSDK untriaged Request triage from a team member labels Oct 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-NetSDK untriaged Request triage from a team member
Projects
None yet
Development

No branches or pull requests

2 participants