Invalid object name 'Customer'. (error 208) means SQL Server could not find a
table or view by that name in the database you are connected to. Either the name is wrong
(here the table is Customers), you are connected to a different database, or
the table lives in a schema your query does not name.
Fix the name, check Database= in the connection string, and write the schema
explicitly (dbo.Customers). The three causes print the same message, so check
all three before you go looking for anything stranger.
The error
PS> .\FixLab.exe 'Server=(localdb)\MSSQLLocalDB;Database=Fix4Lab;Integrated Security=true;TrustServerCertificate=true' 'SELECT Id, Name FROM Customer'
ex.Number = 208
Microsoft.Data.SqlClient.SqlException: Invalid object name 'Customer'.
Why it happens
SQL Server resolves a table name at the moment it compiles the statement, inside the
database the connection is using. An unqualified name like Customer is looked up
in your default schema first and then in dbo; if neither has an object with
that exact name, compilation stops with error 208. The test database had a table called
Customers, so the singular name failed. Singular versus plural is the classic
case with EF Core, whose table names come from the DbSet property name unless
you configure them.
The second cause is the wrong database. The correct table name, Customers, run
against Database=master, failed with the same number and message:
PS> .\FixLab.exe 'Server=(localdb)\MSSQLLocalDB;Database=master;Integrated Security=true;TrustServerCertificate=true' 'SELECT Id, Name FROM Customers'
ex.Number = 208
Microsoft.Data.SqlClient.SqlException: Invalid object name 'Customers'.
The third is a schema: a table created as sales.Customers is not found by a bare
Customers unless sales is your default schema.
The fix
PS> .\FixLab.exe 'Server=(localdb)\MSSQLLocalDB;Database=Fix4Lab;Integrated Security=true;TrustServerCertificate=true' 'SELECT Id, Name FROM dbo.Customers'
1 | Maria Garcia
OK (-1 row(s) affected)
The right database, the real table name and an explicit schema returned the row. To see
what the database actually contains, run
SELECT SCHEMA_NAME(schema_id), name FROM sys.tables in the same connection, and
SELECT DB_NAME() to confirm which database that connection is in. In EF Core,
map the name with ToTable("Customers") or [Table("Customers")]
when it differs from the property name.
How it was reproduced
A console app from dotnet new console -n FixLab -f net10.0 (.NET SDK 10.0.401)
with Microsoft.Data.SqlClient 7.1.1, which runs the SQL in its second argument and prints
ex.Number and the message on failure. A test database Fix4Lab with
one table, dbo.Customers, holding a fictional row, was created with sqlcmd 16 on
SQL Server 2022 LocalDB 16.0.1200.5 on Windows 11. The singular name and the wrong database
each produced error 208; the qualified name in the right database worked.
Frequently asked
- What does Invalid object name mean in SQL Server?
- SQL Server could not find a table or view with that name in the current database. The name is misspelled, the connection is using a different database, or the object is in a schema the query does not name.
- Why do I get Invalid object name when the table exists?
- Usually the connection points at another database, often master because Database= is missing, or the table is in a schema other than dbo. Run SELECT DB_NAME() and query sys.tables in the same connection.
- How do I fix Invalid object name with EF Core?
- EF Core names the table after the DbSet property unless told otherwise. If the real table has another name, map it with ToTable or the Table attribute, or apply the migrations so the expected table exists.
More decoded errors in the Fixes category.