5

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.

Mike S
  • 449
  • 1
  • 5
  • 9
  • 1
    Running into the same issue, and I believe it started with VS2022 update 17.2. My t4 worked fine up to 17.1.x. I have since successfully applied the workaround mentioned in https://stackoverflow.com/a/71822741/46626 to a couple of other projects which were targeting .Net 4.x, but that does not seem to work for a .Net 6 project. – M-Peror May 17 '22 at 21:24
  • 1
    Found a workaround for now. Use "Debug T4 template" instead of "Run custom tool" from the .tt context menu in solution explorer. – M-Peror May 17 '22 at 21:44
  • 1
    Yes, I should have noted that I can still run the T4 templates if I debug them rather than "Run Custom Tool". This is obviously not a desirable solution. – Mike S May 18 '22 at 14:06
  • 2
    Agreed. I have since copied the EF6.Utility.CS.ttinclude into my project directory and made changes there. This did fix the issue - the tt file seems to prefer the local ttinclude over the one from VS. Still not desirable however, because this freezes the ttinclude file and will miss out on any updates or fixes it'll get with a VS update. But better than the "Debug" workaround IMO. – M-Peror May 18 '22 at 19:29

2 Answers2

0

I replaced the following line ...

dte = (EnvDTE.DTE) hostServiceProvider.GetService(typeof(EnvDTE.DTE));

with

dte =  (EnvDTE.DTE) hostServiceProvider.GetCOMService(typeof(EnvDTE.DTE));
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
0

This issue now appears to be resolved in VS 17.3.0

Mike S
  • 449
  • 1
  • 5
  • 9