dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.65k stars 3.15k forks source link

How do I prevent Entity Framework Core tools 2.1.4 making modifications to the target project file? #14143

Closed craigmoliver closed 1 year ago

craigmoliver commented 5 years ago

When running Scaffold-DbContext the .csproj for the target project is modified to include a new <ItemGroup> containing a <Compile Include="path\model-file.cs"> for each Model generated. This behavior is new to version Entity Framework Core tools 2.1.4 and not desired. Previous version did not do this. How do I prevent this behavior?

I have a bounty on Stackoverflow for the answer: https://stackoverflow.com/questions/53674984/how-do-i-prevent-entity-framework-core-tools-2-1-4-making-modifications-to-the-t

Steps to reproduce

Running Scaffold-DbContext via Package Manager Console:

Scaffold-DbContext "Data Source=(localdb)\ProjectsV13;Initial Catalog=DatabaseProjectName;Integrated Security=True;Pooling=False;Connect Timeout=30" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Temp -Tables Table1,Table2,Table3 -Context AppDbContext -Force -StartupProject ProjectName

Further technical details

EF Core version: 2.1.4 Database Provider: Microsoft.EntityFrameworkCore.SqlServer Operating system: Windows 10 IDE: Visual Studio 2017 15.4

smitpatel commented 5 years ago

Are you using old/new format for csproj? Is there any issue in the file getting added to csproj?

craigmoliver commented 5 years ago

I'm not 100% sure how to determine if it is the old or new version. How would one do that quickly?

Issue in the file? I assume you mean the models and no they are fine. It is just the fact that the files are getting added to the project file at all.

smitpatel commented 5 years ago

Just post your csproj here. I can tell which format it is using.

I meant to say, what is the issue if the files are being added to the csproj. Those files are anyway supposed to be part of target project.

craigmoliver commented 5 years ago
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <Configurations>Debug;Release</Configurations>
  </PropertyGroup>

  <ItemGroup>
    <Compile Remove="Temp\**" />
    <EmbeddedResource Remove="Temp\**" />
    <None Remove="Temp\**" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.1.4" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.1.4" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.4" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.1.6" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.1.4">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
    </PackageReference>    
    <PackageReference Include="Microsoft.Extensions.Configuration" Version="2.1.1" />
    <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.1.1" />
    <PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.1.1" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.1" />
    <PackageReference Include="Z.EntityFramework.Plus.EFCore" Version="1.8.18" />
  </ItemGroup>

  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.1.1" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\ReferencedProject\ReferencedProject.csproj" />
  </ItemGroup>

</Project>
smitpatel commented 5 years ago

We just call into VS APIs here https://github.com/aspnet/EntityFrameworkCore/blob/5d889997ca6496d4aa3e4030bdb02ba351856d47/src/EFCore.Tools/tools/EntityFrameworkCore.psm1#L364 It's upto VS to modify csproj.

Since this is newer csproj format, addition of file should not be required to csproj.

craigmoliver commented 5 years ago

If it is not required then why is it happening?

smitpatel commented 5 years ago

@craigmoliver - It is required to be added for old csproj format. But not for new one. Since it is required, EF Core calls into VS project system APIs. Which should do appropriate thing with each project file format. If at all this is bug then it is not in control of EF anyway.

ajcvickers commented 5 years ago

@craigmoliver I have not been able to reproduce this. Could you post a project/solution that exhibits the behavior you are seeing?

ajcvickers commented 5 years ago

EF Team Triage: Closing this issue as the requested additional details have not been provided and we have been unable to reproduce it.

BTW this is a canned response and may have info or details that do not directly apply to this particular issue. While we'd like to spend the time to uniquely address every incoming issue, we get a lot traffic on the EF projects and that is not practical. To ensure we maximize the time we have to work on fixing bugs, implementing new features, etc. we use canned responses for common triage decisions.