Click here to Skip to main content
15,845,565 members
Articles / Entity Framework
Tip/Trick

VS2022, ver. 17.6.2 – EntityFramework6 Broken

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
6 Jun 2023CPOL 6.8K   2   2
Fix for adding new DB tables does not work in VS 2022, ver. 17.6.2, EF6 (Classic)
In Visual Studio 2022, ver. 17.6.2, Entity Framework 6 (Classic) is broken, adding new DB tables in the “Database-First” approach does not work. We are showing how to manually fix it.

Symptoms

  • In Visual Studio 2022, ver. 17.6.2, Entity Framework 6 (Classic) is broken, adding new DB tables in the “Database-First” approach does not work.
  • You get build error:
    Running transformation: System.NullReferenceException: 
    Object reference not set to an instance of an object.

Image 1

Reason

Seems like a bug in Visual Studio 2022, ver. 17.6.2.

Manual Fix

I tried steps from [1] by Chirag Prajapati and they worked:

====================================
Start Notepad in administrator mode, and open the file 
(substituting Community for Professional or Enterprise depending on your version):
C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\Extensions\
Microsoft\Entity Framework Tools\Templates\Includes\EF6.Utility.CS.ttinclude

Update the following code in DynamicTextTransformation (approx line 1920) and change:

_generationEnvironment = type.GetProperty("GenerationEnvironment", 
                         BindingFlags.Instance | BindingFlags.NonPublic);

to

_generationEnvironment = type.GetProperty("GenerationEnvironment", 
BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
=================================================

Click to enlarge image

Click to enlarge image

Conclusion

Will please somebody post when that bug is fixed?

Reference

History

  • 6th June, 2023: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Serbia Serbia
Mark Pelf is the pen name of just another Software Engineer from Belgrade, Serbia.
My Blog https://markpelf.com/

Comments and Discussions

 
QuestionEntityFrameWork6 Broken Pin
vladimir Cavalcanti7-Jun-23 6:00
vladimir Cavalcanti7-Jun-23 6:00 
I found this out using linq to sql. And I discovered that the problem is in the dbname.designer.cs file.
When there are changes or additions to the tables, this line is lost.
#endregion

public DatabaseNameDataContext() :
base("Data Source=Server;Initial Catalog=databaseIntegrated Security=True, mappingSource)
{
OnCreated();
}


staying only

public DatabaseNameDataContext(string connection) :
base(connection, mappingSource)
{
OnCreated();
}

public DatabaseNameDataContext(System.Data.IDbConnection connection) :
base(connection, mappingSource)
{
OnCreated();
}

public DatabaseNameDataContext(string connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
base(connection, mappingSource)
{
OnCreated();
}

public DatabaseNameDataContext(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
base(connection, mappingSource)
{
OnCreated();
}

So, the solution is return the line:
public DatabaseNameDataContext() :
base("Data Source=Server;Initial Catalog=databaseIntegrated Security=True, mappingSource)
{
OnCreated();
}

QuestionI see Pin
BorisL20007-Jun-23 1:01
BorisL20007-Jun-23 1:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.