diff --git a/src/GUI/EFCorePowerTools/EFCorePowerToolsPackage.cs b/src/GUI/EFCorePowerTools/EFCorePowerToolsPackage.cs index 8185e4730..a3e48ff76 100644 --- a/src/GUI/EFCorePowerTools/EFCorePowerToolsPackage.cs +++ b/src/GUI/EFCorePowerTools/EFCorePowerToolsPackage.cs @@ -108,6 +108,11 @@ internal TView GetView() return extensionServices.GetService(); } + internal async Task VisualStudioVersionAsync() + { + return await VS.Shell.GetVsVersionAsync(); + } + protected override async System.Threading.Tasks.Task InitializeAsync(CancellationToken cancellationToken, IProgress progress) { try @@ -292,11 +297,6 @@ private static bool IsConfigFile(string itemName) itemName.EndsWith(".config.json", StringComparison.OrdinalIgnoreCase); } - private async Task VisualStudioVersionAsync() - { - return await VS.Shell.GetVsVersionAsync(); - } - #pragma warning disable VSTHRD100 // Avoid async void methods private async void OnReverseEngineerConfigFileMenuBeforeQueryStatus(object sender, EventArgs e) #pragma warning restore VSTHRD100 // Avoid async void methods diff --git a/src/GUI/Shared/DAL/DotNetAccess.cs b/src/GUI/Shared/DAL/DotNetAccess.cs index 45da26a56..a99c974ed 100644 --- a/src/GUI/Shared/DAL/DotNetAccess.cs +++ b/src/GUI/Shared/DAL/DotNetAccess.cs @@ -5,6 +5,6 @@ namespace EFCorePowerTools.DAL { public class DotNetAccess : IDotNetAccess { - string IDotNetAccess.GetExtensionVersion() => FileVersionInfo.GetVersionInfo(typeof(EFCorePowerToolsPackage)?.Assembly.Location).FileVersion ?? null; + string IDotNetAccess.GetExtensionVersion() => FileVersionInfo.GetVersionInfo(typeof(EFCorePowerToolsPackage).Assembly.Location).FileVersion ?? null; } } diff --git a/src/GUI/Shared/DAL/VisualStudioAccess.cs b/src/GUI/Shared/DAL/VisualStudioAccess.cs index aefde40e2..a27355b85 100644 --- a/src/GUI/Shared/DAL/VisualStudioAccess.cs +++ b/src/GUI/Shared/DAL/VisualStudioAccess.cs @@ -21,10 +21,12 @@ DatabaseConnectionModel IVisualStudioAccess.PromptForNewDatabaseConnection() info = await VsDataHelper.PromptForInfoAsync(); }); +#pragma warning disable S2583 // Conditionally executed code should be reachable if (info == null) { return null; } +#pragma warning restore S2583 // Conditionally executed code should be reachable if (info.DatabaseType == DatabaseType.Undefined) { diff --git a/src/GUI/Shared/Extensions/ProjectExtensions.cs b/src/GUI/Shared/Extensions/ProjectExtensions.cs index 6396b8b35..32787cce0 100644 --- a/src/GUI/Shared/Extensions/ProjectExtensions.cs +++ b/src/GUI/Shared/Extensions/ProjectExtensions.cs @@ -91,10 +91,12 @@ public static List GetConfigFiles(this Project project) result.AddRange(files .Where(f => !f.Contains("\\bin\\") && !f.Contains("\\obj\\"))); +#pragma warning disable S2583 // Conditionally executed code should be reachable if (result.Count == 0) { result.Add(Path.Combine(projectPath, "efpt.config.json")); } +#pragma warning restore S2583 // Conditionally executed code should be reachable return result.OrderBy(s => s).ToList(); } diff --git a/src/GUI/Shared/Handlers/ReverseEngineer/ReverseEngineerHandler.cs b/src/GUI/Shared/Handlers/ReverseEngineer/ReverseEngineerHandler.cs index 9ebdd54e3..af952b1cd 100644 --- a/src/GUI/Shared/Handlers/ReverseEngineer/ReverseEngineerHandler.cs +++ b/src/GUI/Shared/Handlers/ReverseEngineer/ReverseEngineerHandler.cs @@ -377,7 +377,15 @@ private async Task ChooseDataBaseConnectionAsync(ReverseEngineerOptions op psd.PublishSchemas(options.Schemas); } - var (usedMode, allowedVersions) = reverseEngineerHelper.CalculateAllowedVersions(options.CodeGenerationMode, await project.GetEFCoreVersionHintAsync()); + var vsVersion = await package.VisualStudioVersionAsync(); + + var (usedMode, allowedVersions) = reverseEngineerHelper.CalculateAllowedVersions(options.CodeGenerationMode, await project.GetEFCoreVersionHintAsync(), vsVersion); + + if (!allowedVersions.Any()) + { + VSHelper.ShowError($".NET 5 and earlier is not supported, and EF Core 8 requires Visual Studio 17.8 or later"); + return false; + } psd.PublishCodeGenerationMode(usedMode, allowedVersions); diff --git a/src/GUI/Shared/Helpers/ReverseEngineerHelper.cs b/src/GUI/Shared/Helpers/ReverseEngineerHelper.cs index 4f8d0fdc8..adc0d9ef7 100644 --- a/src/GUI/Shared/Helpers/ReverseEngineerHelper.cs +++ b/src/GUI/Shared/Helpers/ReverseEngineerHelper.cs @@ -126,7 +126,7 @@ public string DropTemplates(string optionsPath, string projectPath, CodeGenerati return string.Empty; } - public (CodeGenerationMode UsedMode, IList AllowedVersions) CalculateAllowedVersions(CodeGenerationMode codeGenerationMode, Version minimumVersion) + public (CodeGenerationMode UsedMode, IList AllowedVersions) CalculateAllowedVersions(CodeGenerationMode codeGenerationMode, Version minimumVersion, Version vsVersion) { var list = new List(); @@ -136,14 +136,14 @@ public string DropTemplates(string optionsPath, string projectPath, CodeGenerati list.Add(new CodeGenerationItem { Key = (int)CodeGenerationMode.EFCore6, Value = "EF Core 6" }); } - if (minimumVersion.Major == 8) + if (minimumVersion.Major == 8 && vsVersion >= new Version(17, 8)) { list.Add(new CodeGenerationItem { Key = (int)CodeGenerationMode.EFCore8, Value = "EF Core 8 (preview)" }); } if (!list.Any()) { - throw new InvalidOperationException(".NET 5 and earlier projects are no longer supported"); + return (codeGenerationMode, list); } var firstMode = list.Select(i => i.Key).First();