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

Simplified output path (UseArtifactsOutput) isn't friendly with ef migrations #30725

Open
virzak opened this issue Apr 18, 2023 · 7 comments
Open

Comments

@virzak
Copy link

virzak commented Apr 18, 2023

File a bug

When using simplified output path adding a migration results in:

Include your code

Run the following

Set up project

dotnet new classlib -o UseArtifactsOutput -f net8.0 --no-restore
cd UseArtifactsOutput
echo "<Project><PropertyGroup><UseArtifactsOutput>true</UseArtifactsOutput></PropertyGroup></Project>" > Directory.Build.props
dotnet add package Microsoft.EntityFrameworkCore.SqlServer --version 8.0.0-preview.3.23174.2
dotnet add package Microsoft.EntityFrameworkCore.Design --version 8.0.0-preview.3.23174.2
dotnet build

Notice that obj directory doesn't exist.

Create DbContext.cs with:

using Microsoft.EntityFrameworkCore;

public class MyDbContext : Microsoft.EntityFrameworkCore.DbContext
{
  public MyDbContext(DbContextOptions<MyDbContext> options) : base(options) { }
}

public class TemporaryDbContextFactory : Microsoft.EntityFrameworkCore.Design.IDesignTimeDbContextFactory<MyDbContext>
{
  // remove static if EF stops working
  public static MyDbContext Create()
  {
    var builder = new DbContextOptionsBuilder<MyDbContext>();
    var connection = @"Data Source=(localdb)\mssqlserverlocaldb;Initial Catalog=UseArtifactsOutput";
    builder.UseSqlServer(connection, opt => opt.CommandTimeout(500));
    var vwContext = new MyDbContext(builder.Options);
    return vwContext;
  }

  public MyDbContext CreateDbContext(string[] args) => Create();
}

This won't work

dotnet ef migrations add First

but this will, though unfriendly

dotnet ef migrations add First --msbuildprojectextensionspath .artifacts/obj/UseArtifactsOutput

Include verbose output

The error when not including msbuildprojectextensionspath is:

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.

Include provider and version information

EF Core version: 8.0.0-preview.3.23174.2
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: 8.0.0-preview.3

@virzak virzak changed the title Simplified output path (UseArtifactsOutput) isn't friendly by ef migrations Simplified output path (UseArtifactsOutput) isn't friendly with ef migrations Apr 18, 2023
@ajcvickers
Copy link
Member

Note from triage: consider as part of #18840.

@KennethHoff
Copy link

Took me like 30 minutes to figure out why this didn't work :|

@YandyZaldivar
Copy link

The workaround of adding the option --msbuildprojectextensionspath artifacts/obj/UseArtifactsOutput is not working for me on latest preview, I am getting the same error described in the original post. In my case the DbContext is in a different assembly than main executable. Any hint other than not use artifacts output for now?

@ArturDorochowicz
Copy link

This is also broken when dotnet ef is executed by the SDK during publish.
My issue there: dotnet/sdk#37422 (I created it not knowing about this one)

@ArturDorochowicz
Copy link

The workaround of adding the option --msbuildprojectextensionspath artifacts/obj/UseArtifactsOutput is not working for me on latest preview,

I think I'm seeing the same in release version. Can't make it work. Ended up disabling artifacts output layout.

@ajcvickers ajcvickers modified the milestone: Backlog Jan 9, 2024
@IgorVyatkin
Copy link

IgorVyatkin commented Feb 9, 2024

It happens because ef tool uses hardcoded path to the obj folder (like Path.Combine(Path.GetDirectoryName(projectFile), "obj")) to create ?.csproj.EntityFrameworkCore.targets files.

This folder can be overridden by --msbuildprojectextensionspath and it works but if we use same project as a project and a startup project only. If we use 2 different projects, it doesn't work because ef writes .targets file to a folder set by --msbuildprojectextensionspath only but a migration creation looks for .target files for both projects.

As a dirty workaround try do not use a separate startup project and use --msbuildprojectextensionspath.

@aradalvand
Copy link

Isn't this basically the same as #23853?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants