Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong select query generated with Entity Framework (mysterious extra column)

I'm playing around trying to get a simple MVC website working. In it I am using a database and Entity Framework to communicate with it (as I have been doing for months on other projects).

Everything has been going fine up until I tried to retrieve from a specific table in my database. If I query that, the select that EF generates mysteriously contains a column that does not exists in that table (or any table in that database). This of course throws an exception complaining about the column not existing:

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Invalid column name 'PerformerFolder_Id'.

I've created a UnitOfWork object that uses the repository pattern to retrieve the data, and to make it as simple as possible, I exposed the DataContext so I could manually query that for testing:

var test = loUnitOfWork.Context.Set<DAL.File>();

The DAL.File is a class generated by the POCO generator:

public partial class File : BaseEntity
{
    public int Id { get; set; }
    public string FilePath { get; set; }
    public string FileName { get; set; }

    public virtual ICollection<FilePerformer> FilePerformerCollection { get; set; }
    public virtual ICollection<FileProperty> FilePropertyCollection { get; set; }

    public File()
    {
        FilePerformerCollection = new List<FilePerformer>();
        FilePropertyCollection = new List<FileProperty>();
        InitializePartial();
    }
    partial void InitializePartial();
}

I do have a PerformerFolder that has an Id column as the primary key, but that is in no way connected to the file table.

If I enable debug logging on the DataContext I see that it runs the following query:

SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[FilePath] AS [FilePath], 
    [Extent1].[FileName] AS [FileName], 
    [Extent1].[PerformerFolder_Id] AS [PerformerFolder_Id]
    FROM [dbo].[File] AS [Extent1]

I've tried searching all file for "PerformerFolder" and other than the logical places (in the PerformerFolder class) it did not turn up anything. It seems to be generated based on something, but I just can't figure out what or where.

Does anyone have any suggestions?

like image 539
DennisC Avatar asked Nov 08 '22 18:11

DennisC


1 Answers

Try searching all code files for PerformerFolder without the 'id' EF adds that to navigation properties on it's own.

as @Giorgi mentioned, it must be a partial class somewhere, or maybe a left over from a previous build ?

like image 70
Marty Avatar answered Nov 14 '22 23:11

Marty