Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Core 3.0 error - SqlException: Invalid column name

I get the following error when I migrate my ASP.NET Core MVC from 2.2 to 3.0

SqlException: Invalid column name 'ContractProfileContractId'.

My statement which throws the error:

var totalemployees =   _context.EmployeeProfile.Where(x => x.Active == true).ToList();

When I add the column ContractProfileContractId to the Employees table, the apps works, but I need to add more and more columns for other tables.

I started coding this app in .NET Core 1.1 (2017), migrated it to 2.0, 2.1 and 2.2 (1 year ago) and now to 3.0.

I tried 3.1 migration and same problem exists.

The application passed the login screen without any error. The error appears only after trying to query from the database using EF.

Any ideas?

like image 777
Ahmad Avatar asked Sep 11 '25 22:09

Ahmad


1 Answers

It look like that EF Core is generating for you a foreign key in your model.

Please check your model and context to see if you have some relationships declared and where the foreign key isn't declared into DBB accordingly to conventions (i-e EntityId for the foreign key).

If so then you can decorate your foreign key property in your POCO class (but i'm pretty sure it could also be configured during the data context definition)

/// The DBB Foreign key property
[ForeignKey("RelationProperty")]
[Required]
public int IdRelationProperty { get; set; }


/// 
public virtual XXXType RelationProperty{ get; set; }
like image 159
T. Grandemange Avatar answered Sep 14 '25 12:09

T. Grandemange