diff --git a/src/dotnet-ef/Project.cs b/src/dotnet-ef/Project.cs index 578550045ce..ee15537fe52 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.IsNullOrWhiteSpace(outputString); + } + 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(),