简体   繁体   English

使用Entity Framework生成错误的选择查询(神秘的额外列)

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

I'm playing around trying to get a simple MVC website working. 我正在努力让一个简单的MVC网站工作。 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. 描述:执行当前Web请求期间发生未处理的异常。 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'. 异常详细信息:System.Data.SqlClient.SqlException:无效的列名称'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: 我创建了一个UnitOfWork对象,它使用存储库模式来检索数据,并使其尽可能简单,我公开了DataContext因此我可以手动查询它以进行测试:

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

The DAL.File is a class generated by the POCO generator: DAL.File是由POCO生成器生成的类:

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. 我有一个PerformerFolder ,它有一个Id列作为主键,但是它没有连接到文件表。

If I enable debug logging on the DataContext I see that it runs the following query: 如果我在DataContext上启用调试日志记录,我会看到它运行以下查询:

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. 我已经尝试在所有文件中搜索“PerformerFolder”而不是逻辑位置(在PerformerFolder类中)它没有发现任何东西。 It seems to be generated based on something, but I just can't figure out what or where. 它似乎是基于某些东西生成的,但我无法弄清楚是什么或在哪里。

Does anyone have any suggestions? 有没有人有什么建议?

Try searching all code files for PerformerFolder without the 'id' EF adds that to navigation properties on it's own. 尝试搜索PerformerFolder所有代码文件而不使用'id'EF将其添加到它自己的导航属性中。

as @Giorgi mentioned, it must be a partial class somewhere, or maybe a left over from a previous build ? 正如@Giorgi所提到的那样,它必须是某个部分的某个类,或者可能是以前版本中遗留下来的?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM