为什么我通过LINQ to Entities通过同一查询生成的SQL运行得到不同的结果?

时间:2014-10-09 03:38:06

标签: sql entity-framework linq-to-entities entity-framework-6

我正在研究上个学期由另一个小组开办的学校项目。本学期我是一个负责完成这个项目的团队。团队之间有ZERO普通人....我的团队是一个全新的团队,试图完成另一个团队项目,几乎没有文档。

无论如何,在背景的情况下,我遇到了项目问题。我的实体框架似乎不喜欢我创建的观点。值得一提的是,在创建此视图时,它是一个复杂的视图,是通过加入大约6-7个表创建的

作为一项任意测试(我真的不需要具有"其中包含#34;的答案),我已经在 SQL Management Studio 中执行了此查询

SELECT *
FROM [dbo].[Course_Answers_Report] -- Course_Answers_Report is a View
WHERE question like '%what%'

产生以下输出:

survey_setup_id | course_number | crn_number | term_offered |        course_title           | Instructor_Name    | question_type_id |                   question                           | answer
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2617            |    107013     |    5001    |    201505    |  Advanced Microsoft Access    | -output ommited-   |      2           | I  understood what the teacher was saying.           |    A
2617            |    107013     |    5001    |    201505    |  Advanced Microsoft Access    | -output ommited-   |      2           | I can apply what I learned in this class.            |    A
2617            |    107013     |    5001    |    201505    |  Advanced Microsoft Access    | -output ommited-   |      2           | I understood what was expected of me in this course. |    A

现在在 Visual Studio 中我有一小段代码(作为一个小小的注释,这是在MVC中,但问题不在于MVC,而是在LINQ的某个地方) ,实体或控制器.....这是通过做一些调试来决定的。

public ActionResult modelTest()
{
        using (SurveyEntities context = new SurveyEntities())
        {
                context.Database.Log = s => System.Diagnostics.Debug.WriteLine(s);

                var questions = context
                                .Course_Answers_Report
                                .Where(r => r.question.Contains("what"))
                                .ToList();

                ViewBag.Questions = questions;
         }
}

这会在View上输出下表(同样,问题肯定不在View中,因为在调试时,保存List的var包含所有不正确的数据)

survey_setup_id | course_number | crn_number | term_offered |        course_title           | Instructor_Name    | question_type_id |                   question                           | answer
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2617            |    107013     |    5001    |    201505    |  Advanced Microsoft Access    | -output ommited-   |      2           | I understood what the teacher was saying.            |    A
2617            |    107013     |    5001    |    201505    |  Advanced Microsoft Access    | -output ommited-   |      2           | I understood what the teacher was saying             |    A
2617            |    107013     |    5001    |    201505    |  Advanced Microsoft Access    | -output ommited-   |      2           | I understood what the teacher was saying.            |    A

正如你所看到的,这个输出是不正确的,因为问题(或者更确切地说是记录)在它应该是

时永远不会改变

此linq语句生成的SQL是

SELECT 
[Extent1].[survey_setup_id] AS [survey_setup_id], 
[Extent1].[course_number] AS [course_number], 
[Extent1].[crn_number] AS [crn_number], 
[Extent1].[term_offered] AS [term_offered], 
[Extent1].[course_title] AS [course_title], 
[Extent1].[Instructor_Name] AS [Instructor_Name], 
[Extent1].[question_type_id] AS [question_type_id], 
[Extent1].[question] AS [question], 
[Extent1].[answer] AS [answer]
FROM (SELECT 
[Course_Answers_Report].[survey_setup_id] AS [survey_setup_id], 
[Course_Answers_Report].[course_number] AS [course_number], 
[Course_Answers_Report].[crn_number] AS [crn_number], 
[Course_Answers_Report].[term_offered] AS [term_offered], 
[Course_Answers_Report].[course_title] AS [course_title], 
[Course_Answers_Report].[Instructor_Name] AS [Instructor_Name], 
[Course_Answers_Report].[question_type_id] AS [question_type_id], 
[Course_Answers_Report].[question] AS [question], 
[Course_Answers_Report].[answer] AS [answer]
FROM [dbo].[Course_Answers_Report] AS [Course_Answers_Report]) AS [Extent1]
WHERE [Extent1].[question] LIKE N'%what%'

当在SQL管理工作室中运行此SQL时,它会产生正确的结果。我不知道为什么EF会以这种方式表现,任何人都可以提供见解

编辑:根据Danny Varod的要求,可以在此处找到EDMX http://pastebin.com/dUf6J4fV ,可在此处找到视图 http://pastebin.com/sCsqNYWc (视图是一种丑陋/草率,因为它应该是一个测试和实验)

1 个答案:

答案 0 :(得分:3)

您的问题在edmx文件中可见;

  

警告6002:表格/视图' wctcsurvey.dbo.Course_Answers_Report'没有定义主键。已推断密钥,并将定义创建为只读表/视图。

<EntityType Name="Course_Answers_Report">
<Key>
    <PropertyRef Name="survey_setup_id" />
</Key>

您尚未在表格中定义主键,因此已经猜到了#34;。由于猜测列survey_setup_id在表中不是唯一的(正确结果中的所有3行具有相同的值),EF将会混淆并获取相同的对象3次(毕竟它具有相同的猜测主键) )。

如果向模型添加正确的主键注释(即唯一字段),则问题将消失。