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

617 Views Asked by At

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

1

There are 1 best solutions below

2
MrMoeinM On BEST ANSWER

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