Closed dglozano closed 2 years ago
Well the problem was on our side. We had some convenience lambda properties in the Project
and Department
defined like this:
Project.cs
...
public List<Department> Departments => ProjectDepartments?.Select(x => x.Department).ToList() ?? Enumerable.Empty<Department>();
...
Department.cs
...
public List<Project> Projects => ProjectDepartments?.Select(x => x.Project).ToList() ?? Enumerable.Empty<Project>();
...
For some reason, those lambda expressions were trying to add the wrong columns to the model. Adding [NotMapped] to those lambda properties solved our problem.
However, as far as I understand, EF6 ignored lambda properties and didn't map them at all. From what I can see in the EF Core documentation, it seems that get only properties (which I believe are equivalent to lambda properties) shouldn't be mapped either in EF Core. Am I understanding this wrong?
@dglozano These are collection navigation properties. If I remember correctly, read-only collection navigations should be discovered used by EF6 as well as EF Core. That being said, if they should not be mapped, then using [NotMapped]
is the right thing to do.
@ajcvickers I have double checked and I can confirm that in EF6 collection lambda properties are also discovered. Didn't know there was a distinction between collection lambda properties and lambda properties. Thanks for the help 😄 I am closing this.
We have recently decided to port an existing application using .NET Framework 4.7 Web API and EF6 to .NET Core and EF Core 3.1.
As a first step, we are going to move to EF Core. Once that's done, we will move later to .NET Core.
We are almost done with the porting to EF Core, but unfortunately we are experiencing an incorrect behavior when using .Include() statements in the Entities we had to create to map N-to-N relationships. The SQL generated is trying to read non existing columns which ends up in a "Invalid column name" error when executing the query in the database.
For example, we have a
Project
entity which has a n-to-n relationship withDepartment
. When porting to EF Core, we needed to add the intermediate entityProjectDepartment
to do the mapping.Project.cs
Department.cs
ProjectDepartment.cs
ProjectConfiguration.cs
DepartmentConfiguration.cs
DepartmentConfiguration.cs
ProjectDepartment.Configurationcs
Now the problem is that whenever we try to use an
Include
from the "join" entity we would get an invalid SQL translation. For example, these will fail:Context.ProjectDepartment.Include(x => x.Department)
Context.ProjectDepartment.Include(x => x.Project)
Context.Department.Include(x => x.ProjectDepartment).ThenInclude(x => Project)
Context.Project.Include(x => x.ProjectDepartment).ThenInclude(x => Department)
The SQL generated is trying to SELECT a ProjectId column in the Department table or a DepartmentId in the Project table when doing those includes, columns which of course don't exist.
Here is a proper example:
IQueryable
Generated SQL
If it didn't try to SELECT that column, then the query would be correct.
Just to point out, that query would have the same problem even if it was simpler (i.e.
Context.Project .Include(x => x.ProjectDepartments) .ThenInclude(x => x.Department).ToList();
)The weird thing is that EVERYTHING else seems to be working fine, the only problem is when using Includes/ThenIncludes in the new entities we had to create to map the N-to-N relationships (for which we had the corresponding DB table already, but we didn't need an entity in EF6).
This is the only thing that's stopping us to finally move to EF Core and we can't figure out what we are doing wrong. I have been trying to find a similar issue but couldn't find anything relevant... this is the only one that was quite similar: https://github.com/dotnet/efcore/issues/4093
It's probably an error on our side, but I don't know where else to look to be honest, so any help will be really appreciated it 😄
DB tables
Further technical details
EF Core version: 3.1.5 Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: NET Framework 4.7 Operating system: Windows 10 IDE: Visual Studio 2019 16.3