sjh37 / EntityFramework-Reverse-POCO-Code-First-Generator

EntityFramework Reverse POCO Code First Generator - Beautifully generated code that is fully customisable. This generator creates code as if you reverse engineered a database and lovingly created the code by hand. It is free to academics (you need a .edu or a .ac email address), not free for commercial use. Obtain your licence from
https://www.reversepoco.co.uk/
Other
700 stars 230 forks source link

Failed to load provider "System.Data.SqlClient" #804

Closed sjh37 closed 1 year ago

sjh37 commented 1 year ago

Upgrading to Visual Studio 17.6 breaks the generator for FileManagerType.VisualStudio (.NET 4.x framework projects)

If you are experiencing this issue, please upvote this Microsoft ticket

.NET Core or SDK-style projects are not affected.

Quick fix

If you are generating the files into a single .cs file (Settings.GenerateSeparateFiles = false) or your project .csproj is the newer SDK style, you can set Settings.FileManagerType = FileManagerType.EfCore; This bypasses the EF6.Utility.CS.ttinclude file management.

Permanent fix

Start Notepad in administrator mode, and open the file (substituting Community for Professional or Enterprise depending on your version): C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\Templates\Includes\EF6.Utility.CS.ttinclude

Update the following code in DynamicTextTransformation (approx line 1920) and change:

_generationEnvironment = type.GetProperty("GenerationEnvironment", BindingFlags.Instance | BindingFlags.NonPublic);

to

_generationEnvironment = type.GetProperty("GenerationEnvironment", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);

Save the EF6.Utility.CS.ttinclude file.

Details of the issue

The Microsoft.VisualStudio.TextTemplating.dll build supplied with Visual Studio 17.6.0 (in \Microsoft Visual Studio\2022\Community\Common7\IDE\PublicAssemblies) changes the visibility of the TextTransformation.GenerationEnvironmentproperty from “protected” to “public”. This breaks the DynamicTextTransformation class in both EF6.Utility.CS.ttinclude and EF6.Utility.VB.ttinclude (both in \Microsoft Visual Studio\2022\Community\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\Templates\Includes).

The DynamicTextTransformation class attempts to access TextTransformation.GenerationEnvironment using reflection with the “Instance” and “NonPublic” binding flags. This fails now that TextTransformation.GenerationEnvironment is public, and DynamicTextTransformation throws a NullReferenceException error.

GregBartlett commented 1 year ago

Thanks for posting this, saved me some headache trying to figure out what was going on. As of June 11th, it looks like Microsoft Addressed it: "A fix for this issue has been internally implemented and is being prepared for release. We'll update you once it becomes available for download." (Phil Allen)

sjh37 commented 1 year ago

Microsoft have now resolved the issue in Visual Studio v17.6.4. More info here.