Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When trying to regenerate/generate entity types EF6.Utilities.CS.ttinclude throws nullreference at line 1928 #2100

Closed
AraHaan opened this issue Mar 20, 2023 · 13 comments

Comments

@AraHaan
Copy link
Member

AraHaan commented Mar 20, 2023

Include your code

The problem happens with my capstone project (college course) so I will need to include the code privately (I can dm it to a maintainer on discord, I am Magic Mage#9260). Basically, expand the project that contains the edmx, expand models, expand the edmx, and then right click the non-context *.tt file and select "Debug T4 Template.". After that notice that the exception is thrown in the file that I noted in the issue name at the exact line. Opening the edmx file and saving it without changes also reproduces the problem with an unhelpful stack trace that the "Debug T4 Template." helps resolve.

Include stack traces

This is the exception I get when the text template tries to generate the entity types:
image

Stack Trace:

Running transformation: System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.VisualStudio.TextTemplatingA5ABE4DEDDB259CC1BBB7344A66833B5C405B93776F35DD6B3989F1A41088892007BA7448761011D5E3DBAEB5B93439FADE79CC07BC15250A1781EBE8A44F37E.GeneratedTextTransformation.DynamicTextTransformation.get_GenerationEnvironment()
   at Microsoft.VisualStudio.TextTemplatingA5ABE4DEDDB259CC1BBB7344A66833B5C405B93776F35DD6B3989F1A41088892007BA7448761011D5E3DBAEB5B93439FADE79CC07BC15250A1781EBE8A44F37E.GeneratedTextTransformation.EntityFrameworkTemplateFileManager..ctor(Object textTransformation)
   at Microsoft.VisualStudio.TextTemplatingA5ABE4DEDDB259CC1BBB7344A66833B5C405B93776F35DD6B3989F1A41088892007BA7448761011D5E3DBAEB5B93439FADE79CC07BC15250A1781EBE8A44F37E.GeneratedTextTransformation.EntityFrameworkTemplateFileManager.VsEntityFrameworkTemplateFileManager..ctor(Object textTemplating)
   at Microsoft.VisualStudio.TextTemplatingA5ABE4DEDDB259CC1BBB7344A66833B5C405B93776F35DD6B3989F1A41088892007BA7448761011D5E3DBAEB5B93439FADE79CC07BC15250A1781EBE8A44F37E.GeneratedTextTransformation.EntityFrameworkTemplateFileManager.Create(Object textTransformation)
   at Microsoft.VisualStudio.TextTemplatingA5ABE4DEDDB259CC1BBB7344A66833B5C405B93776F35DD6B3989F1A41088892007BA7448761011D5E3DBAEB5B93439FADE79CC07BC15250A1781EBE8A44F37E.GeneratedTextTransformation.TransformText()

Possible Workarounds

Because EFCore 7 has templating support, I am considering creating a dummy project that uses .NET 6 and references EFCore 7 (and uses dotnet ef) just for generating the scaffolded code and then manually add the code files into the .NET Framework project (as it should still compile under .NET Framework with similar code. Ironically none of the edmx issues I been having recently happen when using EFCore so my owner classmates working on the same project with me are also considering it just so we can fix the edmx issues we are facing for good due to time restraints.

Include verbose output

Currently because this happens with the text templates, I do not think it's possible to do verbose output. However, I did not try deleting all of the packages and then force restore them again in case of corrupted package data yet though.

Include provider and version information

EF version: 6.4.4
Database provider: SQL Server
Target framework: net472
Operating system: Windows 11
IDE: Visual Studio 2022 latest preview (17.6 preview 2)

@ajcvickers
Copy link
Member

This issue has been closed because EF6 is no longer being actively developed. We are instead focusing on stability of the codebase, which means we will only make changes to address security issues. See the repo README for more information.

@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Mar 20, 2023
@AraHaan
Copy link
Member Author

AraHaan commented Mar 20, 2023

But isn't a failing ttinclude file also count as instability? While it does have a workaround, it is kind of annoying when you are somewhat pressed for time.

@IanKemp
Copy link

IanKemp commented May 17, 2023

@AraHaan My only question would be, "why are you using EF 6 in 2023"?

Also, considering this happened after a Visual Studio upgrade, you should probably file an issue with the Visual Studio team.

@AraHaan
Copy link
Member Author

AraHaan commented May 17, 2023

@AraHaan My only question would be, "why are you using EF 6 in 2023"?

Also, considering this happened after a Visual Studio upgrade, you should probably file an issue with the Visual Studio team.

I managed to do an alternative solution, also it was for a college project that forces EF6 because of the fact that efcore cannot be consumed in .NET Framework projects since efcore v3. And .NET Framework was a requirement because nobody with administrator access at the college was going to approve of installing the .NET Core/.NET 5+ extensions to IIS to allow .NET Core/.NET 5+ to be used with IIS as they were planning to get new servers this year anyways after the semester sometime.

So, I managed to stumble upon a way to read the edmx format, and basically source generate with an incremental generator that then generates code for both ef6 and it uses all of the dataannotations attributes which makes it compatible for efcore itself (which it actually does change the generated DbContext class when an efcore nuget package gets referenced in project).

https://github.com/Elskom/EdmxSourceGenerator is the source generator I made to do such a thing, it seems to also work perfectly as well, and I do not need to worry about the error prone text templates anymore. Infact, it works so well that I think it could become a new default for EF6 and for EFCore as it also self-sets itself up for usage as long as the files it expects exists.

@AYColumbia
Copy link

AYColumbia commented May 22, 2023

@AraHaan,
Am running into the same issue now even though generating the poco worked fine a couple months ago. I'm on FW 4.8.1 using EF 6.4.4 in vb.net. Do you think your solution above would work for me? If so, would you kindly provide a "how to" steps in using it to resolve the issue? Thanks much.

BTW, the culprit is the line below returning Nothing (null).
_generationEnvironment = type.GetProperty("GenerationEnvironment", BindingFlags.Instance Or BindingFlags.NonPublic)

2023-05-22 11_29_53-t4 template ef 6 4 4 null exception

@ErikEJ
Copy link
Contributor

ErikEJ commented May 22, 2023

Have you tried the latest VS preview?

@AYColumbia
Copy link

AYColumbia commented May 23, 2023

Have you tried the latest VS preview?

Nope. Using VS 2022 Pro on latest update. I'm going to file a bug report with the VS team to see if they can get this fixed in a timely manner. Probably one of the updates broke it since it worked just fine for a long time.

@AYColumbia
Copy link

AYColumbia commented May 24, 2023

Hi, just wanted to report back that it's now working. Not sure what I did but I did something to probably cause visual studio to regenerate and remove what it probably had in cache. I do still have the issue on my other dev laptop and cannot get that to work at all for version 17.6.2. I'm having to use VS 2019 for the data layer project just to generate the classes for now.

Crazy stuff.

@AraHaan
Copy link
Member Author

AraHaan commented May 30, 2023

So, that is what broke it. Well the source generator I posed above currently works for C# only, however I would be ok if someone pull requests in VB.NET support to it as well. I do personally feel like I should migrate it away from using stringbuilders to generate the actual code though.

@AYColumbia
Copy link

I appreciate you writing back. It's a pain but no problem at this point. It's a hassle as I have the data layer project in 2019 now just to gen the classes until it gets fixed in 2022. A LOT of people are reporting this so am not sure why they haven't jumped on it as it's a severe bug. I only work in VB.NET because that's my client's project as I normally work in C#.

I'll have to update my post above because I was in error in the code snippet because that's from the built-in .tt file from EF6 and NOT my generated code. I hadn't realized the debugger had loaded the included .tt file and not mine.

@AraHaan
Copy link
Member Author

AraHaan commented Aug 12, 2023

I appreciate you writing back. It's a pain but no problem at this point. It's a hassle as I have the data layer project in 2019 now just to gen the classes until it gets fixed in 2022. A LOT of people are reporting this so am not sure why they haven't jumped on it as it's a severe bug. I only work in VB.NET because that's my client's project as I normally work in C#.

I'll have to update my post above because I was in error in the code snippet because that's from the built-in .tt file from EF6 and NOT my generated code. I hadn't realized the debugger had loaded the included .tt file and not mine.

Feel free to open an issue in my EdmxSourceGenerator project for VB.NET support and I will consider it. I think I also added support for emitting efcore code as well.

Edit: Yep I did when it sees the EFCore namespace in the "compilation".

@AYColumbia
Copy link

I appreciate that but no need to waste your time at this point. Microsoft provided a "fix" for vb.net template but left a semi-colon in the fixed code that breaks it. LOL Thankfully removing the semi-colon from the template does the trick and is now working under VS 2022.

@AraHaan
Copy link
Member Author

AraHaan commented Aug 12, 2023

I actually prefer the source generator more though and find that it is far better and performant than the template with better features. Also I sorta shared it with my capstone project to companies that were there and they sorta liked the project and found it to be a great thing even for them to use over Microsoft’s text templates that can break at any time. Also works inbox without need to remember to delete extra semicolons. Also it is technically not wasting my time as broken text templates wasted it far more when I was in my capstone class.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants