From 344675245d4ba67a8cb582ac5214b80e268399fb Mon Sep 17 00:00:00 2001 From: Andriy Svyryd Date: Thu, 4 Dec 2025 14:13:48 -0800 Subject: [PATCH 1/2] Allow tools to run on projects targeting multiple frameworks. Fixes #37230 --- src/dotnet-ef/Project.cs | 20 +++++++++++++++++++ .../Properties/Resources.Designer.cs | 8 +++++++- src/dotnet-ef/Properties/Resources.resx | 3 +++ src/dotnet-ef/RootCommand.cs | 6 +++++- 4 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/dotnet-ef/Project.cs b/src/dotnet-ef/Project.cs index 578550045ce..8e9ea0cc8dc 100644 --- a/src/dotnet-ef/Project.cs +++ b/src/dotnet-ef/Project.cs @@ -85,6 +85,11 @@ public static Project FromFile( var exitCode = Exe.Run("dotnet", args, handleOutput: line => output.AppendLine(line)); if (exitCode != 0) { + if (framework == null && HasMultipleTargetFrameworks(file)) + { + throw new CommandException(Resources.MultipleTargetFrameworks); + } + throw new CommandException(Resources.GetMetadataFailed); } @@ -131,6 +136,21 @@ private record class ProjectMetadata public Dictionary[]> Items { get; set; } = null!; } + private static bool HasMultipleTargetFrameworks(string file) + { + var args = new List { "msbuild", "/getProperty:TargetFrameworks", file }; + + var output = new StringBuilder(); + var exitCode = Exe.Run("dotnet", args, handleOutput: line => output.AppendLine(line)); + if (exitCode != 0) + { + return false; + } + + var outputString = output.ToString(); + return !string.IsNullOrEmpty(outputString) && !outputString.IsWhiteSpace(); + } + private static void CopyBuildHost( Dictionary[] runtimeCopyLocalItems, string targetDir) diff --git a/src/dotnet-ef/Properties/Resources.Designer.cs b/src/dotnet-ef/Properties/Resources.Designer.cs index 9c2b87d72b2..425fb441247 100644 --- a/src/dotnet-ef/Properties/Resources.Designer.cs +++ b/src/dotnet-ef/Properties/Resources.Designer.cs @@ -317,6 +317,12 @@ public static string MultipleProjectsInDirectory(object? projectDir) public static string MultipleStartupProjects => GetString("MultipleStartupProjects"); + /// + /// The project targets multiple frameworks. Use the --framework option to specify which target framework to use. + /// + public static string MultipleTargetFrameworks + => GetString("MultipleTargetFrameworks"); + /// /// The namespace to use. Matches the directory by default. /// @@ -324,7 +330,7 @@ public static string NamespaceDescription => GetString("NamespaceDescription"); /// - /// Additionally generate all the code required for NativeAOT compilation and precompiled queries (experimental). + /// Generate additional code in the compiled model required for NativeAOT compilation and precompiled queries (experimental). /// public static string NativeAotDescription => GetString("NativeAotDescription"); diff --git a/src/dotnet-ef/Properties/Resources.resx b/src/dotnet-ef/Properties/Resources.resx index 38d2f89eed8..ef4e49c1f06 100644 --- a/src/dotnet-ef/Properties/Resources.resx +++ b/src/dotnet-ef/Properties/Resources.resx @@ -264,6 +264,9 @@ More than one project was found in the current working directory. Use the --startup-project option. + + The project targets multiple frameworks. Use the --framework option to specify which target framework to use. + The namespace to use. Matches the directory by default. diff --git a/src/dotnet-ef/RootCommand.cs b/src/dotnet-ef/RootCommand.cs index 792139f9598..686ea398b30 100644 --- a/src/dotnet-ef/RootCommand.cs +++ b/src/dotnet-ef/RootCommand.cs @@ -66,7 +66,11 @@ protected override int Execute(string[] _) Reporter.WriteVerbose(Resources.UsingProject(projectFile)); Reporter.WriteVerbose(Resources.UsingStartupProject(startupProjectFile)); - var project = Project.FromFile(projectFile); + var project = Project.FromFile( + projectFile, + _framework!.Value(), + _configuration!.Value(), + _runtime!.Value()); var startupProject = Project.FromFile( startupProjectFile, _framework!.Value(), From 7402e513a14c42242df97276dafc5e0767713859 Mon Sep 17 00:00:00 2001 From: Andriy Svyryd Date: Thu, 4 Dec 2025 15:31:23 -0800 Subject: [PATCH 2/2] Update src/dotnet-ef/Project.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/dotnet-ef/Project.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dotnet-ef/Project.cs b/src/dotnet-ef/Project.cs index 8e9ea0cc8dc..ee15537fe52 100644 --- a/src/dotnet-ef/Project.cs +++ b/src/dotnet-ef/Project.cs @@ -148,7 +148,7 @@ private static bool HasMultipleTargetFrameworks(string file) } var outputString = output.ToString(); - return !string.IsNullOrEmpty(outputString) && !outputString.IsWhiteSpace(); + return !string.IsNullOrWhiteSpace(outputString); } private static void CopyBuildHost(