❌ Invalid column name SqlException / Entity Framework
❓ What is "Invalid column name"?
This error is thrown when you execute a SQL query (or an EF Core query that generates SQL) that references a column that does not exist in the underlying database table (or view) at the time of execution.
⚠️ Typical Error Messages
System.Data.SqlClient.SqlException: Invalid column name 'SomeColumn'.
-- or --
Microsoft.Data.SqlClient.SqlException: Invalid column name 'SomeColumn'.
-- or in EF Core --
System.InvalidOperationException: 'SomeColumn' is not a valid property name.
This can happen in many contexts: when using SqlCommand, LINQ queries, EF Core's FromSqlRaw, stored procedures, or even when EF Core tries to generate a migration.
It’s a sign that your database schema and your code (or model) are out of sync.
🔍 Root Causes
Common causes include:
The column name in your query or model is misspelled or has a different case (if using case‑sensitive collation).
The database was modified (via SQL script or manual) but your model or query wasn't updated.
You added a property but forgot to create and apply a migration.
In a JOIN, you might reference a column that belongs to a table not included in the query.
Using FromSqlRaw and projecting a column that doesn't exist in the result set.
JSON columns or computed columns may require special handling.
🛠️ Step‑by‑Step Fixes
Follow this process to resolve the error:
Connect to your database (SQL Server Management Studio, Azure Data Studio, or SELECT * FROM INFORMATION_SCHEMA.COLUMNS) and check the exact column name.
If your database uses a case‑sensitive collation, ensure the casing matches exactly.
If you added a property, run dotnet ef migrations add and dotnet ef database update.
Rename the column reference to match the actual database column name.
FromSqlRaw
If you use custom SQL, ensure the result set includes columns that map to your entity properties.
✅ Quick Tip
In Visual Studio, use the SQL Server Object Explorer to view the table columns. For EF Core, compare your model with the generated migration file.
📝 Direct SQL and Stored Procedures
If you're writing raw SQL or calling a stored procedure, the error is straightforward — the column doesn't exist in the table or view you're querying.
For stored procedures, ensure the procedure returns a column with the expected name. You may need to use AS to alias the result.
💡 Tip
Always test your SQL queries directly in SSMS or Azure Data Studio before running them from code.
🧩 Entity Framework Core
In EF Core, this error often appears when you query an entity that has a property not mapped to a database column, or when the column name in the model doesn't match the database.
Model vs. Database column mismatch
Missing property in migration
If you added a new property to your entity but forgot to create a migration, the column won't exist in the database. Run:
🔄 Migrations and Schema Synchronization
One of the most common causes is that your database schema is out of sync with your EF Core model. This can happen if:
- You manually changed the database without creating a migration.
- You have pending migrations that haven't been applied.
- You're using a different database environment (e.g., development vs. production).
Check pending migrations:
If you want to generate a migration that only adds the missing column, you can use the --no-build flag or --no-migrations when adding.
⚠️ Important
If you're in a team environment, ensure all team members have the same migrations applied. Use dotnet ef database update after pulling changes.
🌐 ASP.NET Core and DTO Mapping
Sometimes the error appears when you try to map a DTO property to a database column that doesn't exist, often when using Select or ProjectTo (AutoMapper).
Also, when using FromSqlRaw with a DTO, ensure the SQL result set has columns that exactly match the DTO's property names (or use aliases).
⚡ Raw SQL with FromSqlRaw / FromSqlInterpolated
When you execute raw SQL and map it to an entity, the column names in the result set must match the entity's property names (unless you use column attributes).
Also, ensure that the number of columns returned matches the number of properties you're mapping.
🧪 Testing and In‑Memory Database
When using the EF Core in‑memory provider for testing, you might encounter "Invalid column name" because the in‑memory database doesn't enforce schema the same way. However, if you're using the SQLite in‑memory provider or a real database, the error is real.
For tests, ensure your test database schema is up‑to‑date (e.g., use EnsureCreated or Migrate).
🛡️ Prevention and Best Practices
Always use EF Core migrations to keep your database in sync with your models.
Match property names to column names, or use [Column] attributes to map.
Prefer LINQ queries, which are checked at compile time (though still need schema correctness).
FromSql with alias carefully
If you must use raw SQL, always alias columns to match your entity properties.
Automatically apply migrations during deployment to catch mismatches early.
Compare your model with the database using EF Core's EnsureCreated or third‑party tools.
🏆 Pro Tip
If you're in a microservices environment, consider using a database migration pipeline where migrations are applied before the new version of the service is deployed. This prevents "Invalid column name" errors when rolling out new features.
0 Comments
thanks for your comments!