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
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.
] 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.
]: 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 "g
Please Login to see the rest of the answer
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:
//Try defining only 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.
Write an Article