Please Login to Continue

Free Software Development Articles.Email List
How to Draw Realistic Photos (E-Book)

How to Draw Realistic Photos (E-Book)

$1

Why Software Development Need Ethics to Succeed (E-Book)

Why Software Development Need Ethics to Succeed (E-Book)

$1

How to Make a Social Media Website in Easy Steps

How to Make a Social Media Website in Easy Steps

$1

ASP.Net Core 2.2 MVC Error g.cshtml.cs: The type or namespace name ErrorViewModel could not be found (are you missing a using directive or an assembly reference). File Error.g.cshtml.cs

ASP.Net Core 2.2 MVC Error g.cshtml.cs: The type or namespace name ErrorViewModel could not be found (are you missing a using directive or an assembly reference). File Error.g.cshtml.cs

ASP.Net Core 2.2 MVC Error g.cshtml.cs: The type or namespace name ErrorViewModel could not be found (are you missing a using directive or an assembly reference). File Error.g.cshtml.cs

 
Problem: When working in the ASP.net core 2.2 MVC project and you decide to scaffold the MVC component from the database first using Entity Framework. The error comes about when you already had DTO database table class Models in a Models Folder and then you decide to generate a new version of Database tables into C# classes, because of some changes made to database tables.

[NB] You might get an error when compiling your project with Razor Engine. This is because Razor Engine still references old files in it's Cache System
[Updated 7/26/2020]: This is because when you create a class, Razor Engine generates a class based on the namespace of your class. That is what that g.cs stands for "generated".

Solution: Don't panic, because if you do, you will block your thinking capacity to resolve the problem.

Follow Steps Below to Resolve the Issue:
- First, understand why this happened, When you create an Asp.Net Core 3.1 Class with a namespace, the generated class with "g.cs" is stored to reference your class. In the g.cs generated Model, there is a namespace in there that references the namespace of your Modal Class. When this error happens, it is normally telling you that the NameSpace stored in the g.cs conflicting with another similar NameSpace in another Model Class or it's simply that it won't find the NameSpace defined int the g.cs at all, if you changed the NameSpace of the Class.

- Carefully review the error List in the Output section of Visual Studio and then examine those files listed in the File tab of the Output Console.

- There is a big chance that those Razor View files do not reference the newly scaffolded/generated DTO's NameSpaces (classes mimicking the Database tables)

- Examine Razor View File shown as [FileName]..g.cshtml.cs and then include this directive: @using ProjectName.FolderWhereYourNewDTOsAreGenerated.ClassFileName does this in all files emitting the exception and the error will go. 
 
- Another solution worth trying is if you defined your Model Name as @modal ProjectName.FolderName.ClassModalName try defining only the ClassModalName. See Below:

YourClassName.cshtml

@modal ProjectName.FolderNameOrNameSpace.YourClassModalName

//Try defining only YourClassModalName

@modal YourClassModalName?

[Call to Action]
[You Don't Need To Login To Leave A Comment]
Please leave a comment explaining how you solved your problem to help other developers have more options. Do good to others.

Other Resources
asp.net Core 3.1 asp.net mvc asp.net core errors asp.net web forms asp.net core tutorial Technology published
Buy Me Coffee

Was this page helpful?

Yes Yes! this solution to the problem was helpful. No No! this solution to the problem was not helpful..



© 2020 - ErnesTech - Privacy