efcore
efcore copied to clipboard
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.
Same here.
I updated all the related ef packages, i updated my t4 templates based on Microsoft.EntityFrameworkCore.Templates .
But the problem is same.
If i remove t4 templates, scaffold is generated correctly.
I am using postgresql.
@risperdal Please post your csproj file.
@risperdal Please post your csproj file.
hello, I have the same problem. the content of my csproj file is as follows. I would be grateful if you can help what we need to do.
@akinsoftyazilim42 You are not using the version 8 packages:
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.9">
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.4" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.Design" Version="1.1.0" />
I'm very sorry. I had taken it back. Now I'm sending you the problematic version. Asnet.Cloud.Data.csproj.zip
@akinsoftyazilim42 Remove Npgsql.EntityFrameworkCore.PostgreSQL.Design. It's an obsolete package.
@akinsoftyazilim42 Remove
Npgsql.EntityFrameworkCore.PostgreSQL.Design. It's an obsolete package.
We uninstalled the Npgsql.EntityFrameworkCore.PostgreSQL.Design package, but the error still persists.
@akinsoftyazilim42 Remove
Npgsql.EntityFrameworkCore.PostgreSQL.Design. It's an obsolete package.
yes. i tried it and now i get the same error.
Same error here, without using any PostgreSQL libraries. Used the Upgrade-function of Visual Studio to update my .NET 7 project to .NET 8. Found out that now SQL Date objects are mapped to DateOnly, wanted to change this using a T4 template as shown here, but I get these errors when trying to scaffold my existing DB tables:
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.MissingMethodException: Method not found: 'System.String Microsoft.EntityFrameworkCore.ScaffoldingModelExtensions.GetDbSetName(Microsoft.EntityFrameworkCore.Metadata.IReadOnlyEntityType)'.
at Microsoft.VisualStudio.TextTemplating752bc159.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
Attached is my .csproj-file: Projectfile.zip
Have you tried with a clean set of the latest t4 templates?
Have you tried with a clean set of the latest t4 templates?
I did, I didn't use templates before (didn't need it) so they are the latest. The DbContext.t4 starts (after the imports) with
if (!ProductInfo.GetVersion().StartsWith("8.0"))
The cause of the error is "Microsoft.VisualStudio.Web.CodeGeneration.Design"
Thanks for info. ;)
Thanks for info. ;)
you are welcome
Thanks for the information, but it didn't work for me.
@bricelam Can you take a look at this?
Removing the reference to Microsoft.VisualStudio.Web.CodeGeneration.Design fixed it for me.
Note for triage: I suspect the issue here is that VS package is bringing Mono.TextTemplating 2.3.1, but EF seems to require 2.2.1.
Notes for triage:
The underlying issue here is that Mono.TextTemplating 2.2.1 loads the assembly into the default load context, which allows it to use types created by the EF tooling inside the template. An intentional change was made to stop loading the assembly in this way, which, as far as I can tell, makes it incompatible with EF Core's use. This is presumably why we didn't update to the new version. Assigning the types directly generates:
System.InvalidCastException: [A]Microsoft.EntityFrameworkCore.Metadata.Internal.Model cannot be cast to [B]Microsoft.EntityFrameworkCore.Metadata.Internal.Model. Type A originates from 'Microsoft.EntityFrameworkCore, Version=42.42.42.42, Culture=neutral, PublicKeyToken=adb9793829ddae60' in the context 'Default' at location 'C:\local\code\efcore\artifacts\bin\EFCore.Design.Tests\Debug\net8.0\Microsoft.EntityFrameworkCore.dll'. Type B originates from 'Microsoft.EntityFrameworkCore, Version=42.42.42.42, Culture=neutral, PublicKeyToken=adb9793829ddae60' in the context '"" Mono.TextTemplating.TemplateAssemblyLoadContext #1' at location 'C:\local\code\efcore\artifacts\bin\EFCore.Design.Tests\Debug\net8.0\Microsoft.EntityFrameworkCore.dll'.
at Microsoft.VisualStudio.TextTemplating827e0a4.GeneratedTextTransformation.Initialize()
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 77
I verified using Reflection calls that loading the assembly as before fixes the issues seen with 2.3.1. See #32495.
We should check with @mhutch that there isn't some simple fix. Beyond that, I think we have a few options here, but none seem great:
- Tell users to pin their
Mono.TextTemplatingpackage to 2.2.1 when using EF - Do something hacky like in the draft PR
- Fork the
Mono.TextTemplatingand stop taking a dependency on the external package
I've added an option to Mono.TextTemplating to allow the host to disable the use of AssemblyLoadContext and revert to the old (current) AppDomain behavior.
I have also made some changes to the loading behavior when using AssemblyLoadContext that may fix the underlying issue and make disabling it unnecessary.
If these solve the issue, I can backport to 2.3.x and make a new release.
@mhutch I tried first with the latest preview package, and that still failed. But then I built packages locally from your main branch and used those, and all the tests pass even without setting DisableAssemblyLoadContext.
Sorry for being slow to test this, and thanks for the quick fix! As soon as a preview package is available, we can update our main branch, which will allow customers to test with our daily build.
@ajcvickers preview package 3.0.0-preview-0045-g9b6fc72764 is now live
All: If you have been hitting this, then please try out the latest daily build of EF Core and let us know if the problem is fixed.
All: If you have been hitting this, then please try out the latest daily build of EF Core and let us know if the problem is fixed.
Do the daily builds now require dotnet 9? I followed the instructions on the linked page but can't seem to get the daily build installed on a dotnet 8 based project
@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.
@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.