Pab*_*ian 3 .net c# entity-framework docker entity-framework-core
我正在使用 Entity Framework Core 5 和 Postgres 设置一个新项目。我的所有项目和我的上下文都在同一个项目中。
\n添加迁移时,我收到此错误:
\n\n\n/src/Api.csproj :错误 MSB4057:项目中不存在目标“GetEFProjectMetadata”。无法检索项目元数据。确保它是基于 MSBuild 的 .NET Core 项目。如果您使用自定义 BaseIntermediateOutputPath 或 MSBuildProjectExtensionsPath 值,请使用 --msbuildprojectextensionspath 选项。
\n
EF Core 版本: 5.0.1\n目标框架: net5.0
\n这些是我尝试过的所有命令:
\ndotnet ef migrations add NewMigration\n\ndotnet ef migrations add NewMigration --msbuildprojectextensionspath \n\ndotnet ef migrations add NewMigration -p ".\\src\\Api.csproj"\n\ndotnet ef migrations add NewMigration -p ".\\src\\Api.csproj" --msbuildprojectextensionspath* \nRun Code Online (Sandbox Code Playgroud)\n我尝试了在互联网上找到的很多选项,但没有一个有效。
\n如果我添加 -v ,最后几行是:
\nUsing project \'/media/pablo/l/Projetos/Web/backend/boilerplate/dotnet_csharp/src/Api.csproj\'.\nUsing startup project \'/media/pablo/l/Projetos/Web/backend/boilerplate/dotnet_csharp/src/Api.csproj\'.\nWriting \'/media/pablo/l/Projetos/Web/backend/boilerplate/dotnet_csharp/src/obj/Api.csproj.EntityFrameworkCore.targets\'...\ndotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=/tmp/tmpbscOwZ.tmp /verbosity:quiet /nologo /media/pablo/l/Projetos/Web/backend/boilerplate/dotnet_csharp/src/Api.csproj\n/media/pablo/l/Projetos/Web/backend/boilerplate/dotnet_csharp/src/Api.csproj : error MSB4057: O destino "GetEFProjectMetadata" n\xc3\xa3o existe no projeto.\nMicrosoft.EntityFrameworkCore.Tools.CommandException: 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.\n at Microsoft.EntityFrameworkCore.Tools.Project.FromFile(String file, String buildExtensionsDir, String framework, String configuration, String runtime)\n at Microsoft.EntityFrameworkCore.Tools.RootCommand.Execute(String[] _)\n at Microsoft.EntityFrameworkCore.Tools.Commands.CommandBase.<>c__DisplayClass0_0.<Configure>b__0(String[] args)\n at Microsoft.DotNet.Cli.CommandLine.CommandLineApplication.Execute(String[] args)\n at Microsoft.EntityFrameworkCore.Tools.Program.Main(String[] args)\nUnable 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.\nRun Code Online (Sandbox Code Playgroud)\nAPI.csproj:
\n<Project Sdk="Microsoft.NET.Sdk.Web">\n\n <PropertyGroup>\n <TargetFramework>net5.0</TargetFramework>\n <DocumentationFile>bin\\Debug\\net5.0\\comments.xml</DocumentationFile>\n <GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>\n <noWarn>1591;1572;1573</noWarn>\n <PublishWithAspNetCoreTargetManifest>false</PublishWithAspNetCoreTargetManifest>\n <CodeAnalysisRuleSet>../roslynator.ruleset</CodeAnalysisRuleSet>\n </PropertyGroup>\n \n <ItemGroup>\n <PackageReference Include="FluentValidation" Version="9.4.0" />\n <PackageReference Include="FluentValidation.AspNetCore" Version="9.4.0" />\n <PackageReference Include="Mapster" Version="7.0.1" />\n <PackageReference Include="Swashbuckle.AspNetCore" Version="5.5.1" />\n <PackageReference Include="Scrutor" Version="3.3.0" />\n <PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.1" />\n <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="5.0.1" />\n <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.1" />\n <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.1" />\n <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="5.0.1" />\n <PackageReference Include="jaeger" Version="0.4.2" />\n <PackageReference Include="OpenTracing.Contrib.NetCore" Version="0.7.1" />\n <PackageReference Include="RestSharp" Version="106.2.1" />\n <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.1" />\n <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="5.0.1" />\n <PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.7" />\n <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="5.0.1" />\n <PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />\n <PackageReference Include="Microsoft.OpenApi" Version="1.2.3" />\n <PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" Version="5.6.3" />\n </ItemGroup>\n\n <ItemGroup>\n <None Include="*.json" CopyToPublishDirectory="Always" CopyToOutputDirectory="Always" />\n <None Include="Locales\\**\\*.json" CopyToPublishDirectory="Always" CopyToOutputDirectory="Always" />\n </ItemGroup>\n</Project>\n\nRun Code Online (Sandbox Code Playgroud)\n程序.cs:
\nusing Microsoft.AspNetCore;\nusing Microsoft.AspNetCore.Hosting;\n\nnamespace Linear.Service_Name.Api\n{\n public static class Program\n {\n public static void Main(string[] args)\n {\n CreateWebHostBuilder(args).Build().Run();\n }\n\n public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>\n WebHost.CreateDefaultBuilder(args).UseStartup<Startup>();\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n启动.cs:
\nusing Linear.Service_Name.DataBase.Entities;\nusing Microsoft.AspNetCore.Builder;\nusing Microsoft.AspNetCore.Hosting;\nusing Microsoft.EntityFrameworkCore;\nusing Microsoft.Extensions.Configuration;\nusing Microsoft.Extensions.DependencyInjection;\nusing System.Reflection;\n\n[assembly: Microsoft.AspNetCore.Mvc.ApiController]\nnamespace Linear.Service_Name.Api\n{\n public class Startup\n {\n private IWebHostEnvironment _environment { get; }\n\n public Startup(IConfiguration _, IWebHostEnvironment environment)\n {\n _environment = environment;\n }\n\n public void ConfigureServices(IServiceCollection services)\n {\n services.AddDbContext<Context>(opt => opt\n .UseNpgsql(EnvVariables.LINEAR_API_DOMAIN_NAME_DB_CONNECTION_STRING)\n );\n services.AddCommonsServices<Context>(new CommonServices\n {\n Env = _environment,\n ConnectionString = EnvVariables.LINEAR_API_DOMAIN_NAME_DB_CONNECTION_STRING,\n Assembly = Assembly.GetExecutingAssembly(),\n Swagger = new CommonServices.SwaggerSettings{\n Version = "v1",\n Title = "Service_Name",\n Description = "API REST do M\xc3\xb3dulo de " + ("Service_Name").ToUpper()\n + " da solu\xc3\xa7\xc3\xa3o SG Web."\n }\n });\n }\n\n public void Configure(IApplicationBuilder app)\n {\n app.UserCommonsMiddlewares(new CommonMiddlewares\n {\n Env = _environment,\n PathBase = EnvVariables.LINEAR_API_SERVICE_NAME_BASE_PATH,\n Swagger = new CommonMiddlewares.SwaggerSettings\n {\n Version = "v1",\n Title = "Service_Name"\n }\n });\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n上下文.cs:
\nusing Linear.Infrastructure.Data.Audit;\nusing Linear.Infrastructure.Data.MultiTenancy;\nusing Microsoft.EntityFrameworkCore;\n\nnamespace Linear.Service_Name.DataBase.Entities\n{\n public class Context : DbContext\n {\n public virtual DbSet<Sample_NamesEntity> Sample_Names { get; set; }\n private readonly IAuditEntity _auditEntity;\n private readonly IMultiTenancy _multiTenancy;\n public Context(DbContextOptions<Context> options, IAuditEntity auditEntity,\n IMultiTenancy multiTenancy)\n : base(options)\n {\n _auditEntity = auditEntity;\n _multiTenancy = multiTenancy;\n }\n #region M\xc3\xa9todos Protegidos\n protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)\n {\n }\n protected override void OnModelCreating(ModelBuilder modelBuilder)\n => modelBuilder.ApplyConfigurationsFromAssembly(typeof(Context).Assembly);\n\n #endregion\n #region M\xc3\xa9todos P\xc3\xbablicos\n public override int SaveChanges()\n {\n _multiTenancy.OnSaveChanges(this);\n _auditEntity.OnSaveChanges(this);\n return base.SaveChanges();\n }\n public int SaveChangesWithoutMultiTenancy()\n {\n _auditEntity.OnSaveChanges(this);\n return base.SaveChanges();\n }\n public int SaveChangesWithoutMultiTenancyAndAudit() => base.SaveChanges();\n #endregion\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n项目下载链接:\n https://drive.google.com/file/d/1YWlm_teyGMjJe193AwrFqe3VAeVVKWLq/view?usp=sharing
\n我已经被这个问题困扰了三个多小时,在互联网上尝试了很多建议,但仍然没有成功,如果我能得到一些帮助,我将不胜感激。
\n经过一些研究后,使用 docker 集成和 EF Core 工具的项目似乎存在问题。
我已经下载了你的代码,这是你的内容Directory.Build.props
<Project>
<PropertyGroup>
<DefaultItemExcludes>$(DefaultItemExcludes);$(MSBuildProjectDirectory)/obj/**/*</DefaultItemExcludes>
<DefaultItemExcludes>$(DefaultItemExcludes);$(MSBuildProjectDirectory)/bin/**/*</DefaultItemExcludes>
</PropertyGroup>
<PropertyGroup Condition="'$(DOTNET_RUNNING_IN_CONTAINER)' == 'true'">
<BaseIntermediateOutputPath>$(MSBuildProjectDirectory)/obj/container/</BaseIntermediateOutputPath>
<BaseOutputPath>$(MSBuildProjectDirectory)/bin/container/</BaseOutputPath>
</PropertyGroup>
<PropertyGroup Condition="'$(DOTNET_RUNNING_IN_CONTAINER)' != 'true'">
<BaseIntermediateOutputPath>$(MSBuildProjectDirectory)/obj/local/</BaseIntermediateOutputPath>
<BaseOutputPath>$(MSBuildProjectDirectory)/bin/local/</BaseOutputPath>
</PropertyGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)
BaseIntermediateOutputPath不在容器内运行时更改默认值会破坏 EF Core 工具体验(*)
由于您的BaseIntermediateOutputPath默认设置已更改为在obj/localdocker 容器外部运行时,您需要做的是让 EF Core CLI 知道在哪里可以找到该文件夹。您可以通过使用--msbuildprojectextensionspath参数来实现这一点。在你的情况下,它会像这样(如这里建议的):
dotnet ef migrations add NewMigration --msbuildprojectextensionspath obj/local
Run Code Online (Sandbox Code Playgroud)
如果您仍然无法使其工作,您可以按照另一期的讨论进行操作。在那里,建议另一个可能的修复方法是稍微更改您的Directory.Build.props和*.csproj文件,使后者如下所示:
<Project> <!-- Note: No Sdk="" here. -->
<PropertyGroup>
<!-- Don't use $(Configuration) since it isn't set yet. -->
<MSBuildProjectExtensionsPath>$(MSBuildProjectDirectory)\_intermediate_\</MSBuildProjectExtensionsPath>
</PropertyGroup>
<!-- MSBuildProjectExtensionsPath must be set before this gets imported. Directory.Build.props is too late. -->
<Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" />
<!-- (Project content goes here) -->
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />
</Project>
Run Code Online (Sandbox Code Playgroud)