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

标签 c# linq entity-framework-6

我正在尝试让一个简单的 MVC 网站正常运行。在其中,我使用数据库和 Entity Framework 与其进行通信(就像我在其他项目上几个月来所做的那样)。

在我尝试从我的数据库中的特定表中检索之前,一切都很好。如果我查询,EF 生成的选择神秘地包含该表(或该数据库中的任何表)中不存在的列。这当然会引发异常,提示该列不存在:

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'.

我创建了一个 UnitOfWork 对象,它使用存储库模式来检索数据,为了尽可能简单,我公开了 DataContext 以便我可以手动查询以进行测试:

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

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();
}

我确实有一个 PerformerFolder,它有一个 Id 列作为主键,但它与文件表没有任何关系。

如果我在 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]

我已经尝试在所有文件中搜索“PerformerFolder”,除了逻辑位置(在 PerformerFolder 类中),它没有找到任何东西。它似乎是基于某种东西生成的,但我就是无法弄清楚是什么或在哪里。

有人有什么建议吗?

最佳答案

尝试在所有代码文件中搜索不带“id”的 PerformerFolder EF 自行将其添加到导航属性。

正如@Giorgi 提到的,它一定是某个地方的部分类,或者可能是以前构建的遗留物?

关于c# - 使用 Entity Framework 生成的错误选择查询(神秘的额外列),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35902433/

相关文章:

C# Linq - 在列表中查找重复值并选择它的 id

c# - 使用外键对多个表进行分组

c# - 使用类库中的 View

c# - 项目运行时在 MVC 项目中重复代码

c# - 如何将现有类库转变为 worker 角色

c# - C#中=>运算符的名称

entity-framework - Entity Framework 6 : is there a way to iterate through a table without holding each row in memory

c# - 代码优先 : Mapping entities to existing database tables

c# - 如何重构 C# 中的日志记录?

c# - StreamReader.ReadLine() 不消耗流