Search code examples
c#databaseentity-framework-coreef-database-firstpackage-manager-console

Unable to cast object of type 'System.DBNull' to type 'System.String' in Scaffold-DbContext .Net Core


I use the following method to use Databases:

Scaffold-DbContext "Server=[serverName] ; Database=[DatabaseName]; user id= [UserId] ; password = [Password];" 
        Microsoft.EntityFrameworkCore.SqlServer 
        -Project "[ProjectName]" -Force 
        -ContextDir "[DirectoryNameOutPut]\Context" 
        -OutputDir "[DirectoryNameOutPut]" 
        -UseDatabaseNames -NoPluralize

But in some databases, I get this error:

Unable to cast object of type 'System.DBNull' to type 'System.String'

This error occurs after the Scaffold-DbContext command is written in the Package Manager Console and I press Enter and does not classify any of the tables.

What can I do to fix this error?

enter image description here


Solution

  • Add -Verbose flag to your Scaffold-DbContext to get more details.

    Scaffold-DbContext "Server=[serverName] ; Database=[DatabaseName]; user id= [UserId] ; password = [Password];" 
            Microsoft.EntityFrameworkCore.SqlServer 
            -Project "[ProjectName]" -Force 
            -ContextDir "[DirectoryNameOutPut]\Context" 
            -OutputDir "[DirectoryNameOutPut]" 
            -UseDatabaseNames -NoPluralize
            -Verbose