Search code examples
c#entity-framework-6t4

VS2022 T4 EF6 Templates in .Net 6.0 projects are suddenly throwing serialization exceptions


I have a .Net 6.0 application (started out as .Net 5.0 in VS2019). I use the EF Core for data access. Our older applications are .Net 4.8, and we have a lot of T4 templates that we used to generate service classes and model classes based on the EDMX (EF6). In the .Net 6.0 application I decided to keep using those templates (slightly modified), so I created one .Net Framework project in my solution and added an EDMX that holds the database info. Then in different projects (service project or model project) I have t4 templates that read the EDMX and generate code files based on the database.

These T4 templates worked flawlessly in VS2019 and I believe that the worked in VS2022 at one point (not 100% sure of this), however after making a recent database change I tried to run the templates and started seeing this error every time:

Running transformation: System.Runtime.Serialization.SerializationException: Type 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProjectItem' in Assembly 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation, Version=17.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable.

The error points to this line in EF6.Utility.CS.ttinclude file:

templateProjectItem = dte.Solution.FindProjectItem(_textTransformation.Host.TemplateFile);

My T4 templates work fine if they are in .Net Framework projects, but this error happens for any T4 template in a .Net 6.0 project.

A couple things: I am able to run the T4 templates if I debug them rather than Run Custom Tool. Also, because this code that throws the error is in the EF6.Utility.CS.ttinclude that is installed with Entity Framework Tools every time VS updates, I can't make any changes to that file.


Solution

  • This issue now appears to be resolved in VS 17.3.0