简体   繁体   English

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

[英]Why am I getting different results via LINQ to Entities that via run of SQL generated by the same query?

I'm working on a school project that was started by another group last semester. 我正在上一个学期的一个学校项目,该项目是另一个学期开始的。 This semester I'm on a team that is tasked with completing this project. 这个学期我在一个团队中负责完成这个项目。 There are ZERO common people between the groups .... my team is a completely new team attempting to finish another teams project with little to no documentation. 小组之间有零度普通人..我的团队是一个全新的团队,试图用很少甚至没有文档的方式完成另一个团队的项目。

Anyway, with that background out of the way, I am having an issue with the project. 无论如何,在没有这种背景的情况下,我对该项目有疑问。 My entity framework seems to not like the views I have created. 我的实体框架似乎不喜欢我创建的视图。 It may also be worth mentioning that when creating this view, it is a complex view and was created by joining about 6-7 tables 值得一提的是,在创建此视图时,它是一个复杂的视图,是通过联接约6-7个表创建的

As an arbitrary test (i dont really need answers that have "what" in them), I have executed this query in SQL Management Studio 作为一项任意测试(我真的不需要其中包含“ what”的答案),我已经在SQL Management Studio中执行了此查询

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

Which produces the following output: 产生以下输出:

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

Now in Visual Studio i have this small bit of code (as a small side note this is in MVC, however the issue doesn't lie in MVC, but rather somewhere in the LINQ, Entity, or Controller.....this has been decided by doing some debugging). 现在在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;
         }
}

This outputs the following table on the View (again, the problem is decidedly not in the View because when debugging, the var that holds the List has all incorrect data) 这将在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

As you can see, this output is incorrect as the question (or rather the record) never changes when it should be 如您所见,此输出是不正确的,因为问题(或记录)在应为

The SQL generated by this linq statement is 此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%'

When this SQL is run inside SQL management studio, it produces proper results. 在SQL Management Studio中运行此SQL时,会产生适当的结果。 I am at a loss as to why EF is behaving this way, can anyone offer insight 我对为什么EF采取这种方式感到困惑,任何人都可以提供见解

EDIT: Per request of Danny Varod, the EDMX can be found here http://pastebin.com/dUf6J4fV and the View can be found here http://pastebin.com/sCsqNYWc (the view is kind of ugly/sloppy as it was just supposed to be a test and experiment) 编辑:每丹尼Varod的要求,EDMX可以在这里找到http://pastebin.com/dUf6J4fV和视图可以在这里找到http://pastebin.com/sCsqNYWc (视图是一种丑恶/马虎,因为它本来应该是一个测试和实验)

Your problem is visible in the edmx file; 您的问题在edmx文件中可见;

warning 6002: The table/view 'wctcsurvey.dbo.Course_Answers_Report' does not have a primary key defined. 警告6002:表/视图'wctcsurvey.dbo.Course_Answers_Report'没有定义主键。 The key has been inferred and the definition was created as a read-only table/view. 已推断出键,并且已将定义创建为只读表/视图。

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

You have not defined a primary key in the table, so one has been "guessed". 您尚未在表中定义主键,因此已被“猜测”。 Since the guessed column survey_setup_id is not unique in the table (all 3 rows in the correct result have the same value), EF will get confused and fetch the same object 3 times (it has the same guessed primary key after all). 由于猜测的列survey_setup_id在表中不是唯一的(正确结果中的所有3行都具有相同的值),因此EF会感到困惑,并且会提取相同的对象3次(毕竟具有相同的猜测的主键)。

If you add a correct primary key annotation to your model (ie a unique field), the problem will disappear. 如果您在模型中添加了正确的主键注释(即唯一字段),问题将消失。

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

相关问题 为什么在函数vs匿名块中执行同一查询时会从两个查询中得到两个不同的结果 - Why am i getting two different results from same query when executing it in a function vs anonymous block 为什么我在子查询和查询中得到不同的结果? - Why am I getting different results on a subquery and query? SQL 通过 Python 查询返回的结果与直接 SQL 查询不同。 为什么? - SQL query via Python returns different results than direct SQL query. Why? linq给实体生成了sql - linq to entities generated sql 想要通过SQL / TSQL获得相同的输出,而我通过LINQ查询得到的结果 - want to get same output by SQL/TSQL what i am getting by the LINQ query 为什么此Linq查询返回的结果与SQL等效的结果不同? - Why does this Linq Query Return Different Results than SQL Equivalent? 为什么我无法从此SQL查询中获得任何结果(MS Access) - Why am I not getting any results from this SQL query (MS Access) 为什么我使用此Linq to Sql方法获取InvalidOperationException? - Why am I getting an InvalidOperationException with this Linq to Sql method? 同样的 SQL 查询在每次运行时都会产生不同的结果。 我该如何解决? - The same SQL query is yielding different results each time it is run. How do I resolve? 对Linq To实体的SQL查询 - SQL Query to Linq To Entities
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM