efcore: Scaffolding doesn't work with the latest version of Mono.TextTemplating

When I updated all versions to 8, T4 started giving the following error.

error : The type 'Microsoft.EntityFrameworkCore.Metadata.IModel' of the parameter 'Model' did not match the type passed to the template

error : The host parameter 'Model' could not be converted to the type 'System.CodeDom.CodeTypeReference' specified in the template

error : Error running transform: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.BadImageFormatException: Could not load file or assembly 'Microsoft.Extensions.DependencyInjection.Abstractions, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. Reference assemblies cannot be loaded for execution. (0x80131058)
File name: 'Microsoft.Extensions.DependencyInjection.Abstractions, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' ---> System.BadImageFormatException: Could not load file or assembly 'Microsoft.Extensions.DependencyInjection.Abstractions, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL'. Reference assemblies cannot be loaded for execution. (0x80131058)
File name: 'Microsoft.Extensions.DependencyInjection.Abstractions, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL' ---> System.BadImageFormatException: Cannot load a reference assembly for execution.
   at System.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyPath(String assemblyPath)
   at Mono.TextTemplating.TemplateAssemblyLoadContext.Load(AssemblyName assemblyName) in /_/Mono.TextTemplating/Mono.TextTemplating/TemplateAssemblyLoadContext.cs:line 64
   at System.Runtime.Loader.AssemblyLoadContext.ResolveUsingLoad(AssemblyName assemblyName)
   at System.Runtime.Loader.AssemblyLoadContext.Resolve(IntPtr gchManagedAssemblyLoadContext, AssemblyName assemblyName)
   at Microsoft.VisualStudio.TextTemplating3837c1b.GeneratedTextTransformation.TransformText()
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
   --- End of inner exception stack trace ---
   at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
   at Mono.TextTemplating.CompiledTemplate.TemplateProcessor.CreateAndProcess(ITextTemplatingEngineHost host, CompiledAssemblyData templateAssemblyData, String templateAssemblyFile, String fullName, CultureInfo culture, String[] referencedAssemblyFiles) in /_/Mono.TextTemplating/Mono.TextTemplating/CompiledTemplate.TemplateExecutor.cs:line 78

Processing 'D:\AKINSOFT\cloud\erpcloud\Libraries\Asnet.Cloud.Data\CodeTemplates\EFCore\DbContext.t4' failed.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 27 (10 by maintainers)

Commits related to this issue

Most upvoted comments

Removing this from the project file fixed the issue for me, without having to use .NET 9:

<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.EntityFrameworkCore" Version="8.0.2" />

@jcummings2 EF9 should not require .NET 9 at this time. If you are seeing an issue with this, can you please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate.

I got it to work with .NET 8 but it was a subtle step that is at best implied on the daily builds instructions related to which version to use. Simply pasting the wildcard 9.0.0-* into the .csproj file didn’t work but the build process complained about no such version available. In other words, the statement on the instructions that “Using wildcards will cause NuGet to pull the latest daily build whenever packages are restored” didn’t seem true in my case.

However, after I went into the NuGet package manager in Visual Studio and selected “Latest prerelease 9.0.0-*” for each package then it would build and run. Maybe I missed a step but hopefully these details will help someone else.

@ajcvickers preview package 3.0.0-preview-0045-g9b6fc72764 is now live

Removing the reference to Microsoft.VisualStudio.Web.CodeGeneration.Design fixed it for me.

The cause of the error is “Microsoft.VisualStudio.Web.CodeGeneration.Design”

t4