实体框架神秘错误 [英] Entity Framework mysterious error

查看:63
本文介绍了实体框架神秘错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助.我不明白为什么从我的实体上下文中

Please help. I don`t understand why from my entity context

var stagesExist = context.WfwDocumentWorkStages
   .Any(it => it.Enabled && it.ExecutionId == execution.Id
   && it.Level == execution.Level && it.ResultId == null);

值的阶段现存为假但是

var stages = context.WfwDocumentWorkStages.Where(it => it.Enabled 
   && it.ExecutionId == execution.Id
   && it.Level == execution.Level).ToList();
bool stagesExist = stages.Any(it=>it.ResultId == null);

valuestagesExist是真实的吗?

value stagesExist is true??

推荐答案

问题是您的映射中的这一行:

The problem is this line in your mapping:

this.HasRequired(t => t.WfwEventResult)

您实际上是在告诉EF关联的FK列永远不会为 null (尽管您已将其设置为 int?,并且具有带有 null 值).请记住,EF在构建SQL查询时会使用元数据信息,在这种情况下,我想查询优化器会决定该查询永远不会返回记录(类似于 .Where(it => false))并生成您会看到一个伪造的SQL查询.

You are effectively telling the EF that the associated FK column will never be null (although you've made it int? and have records with null value). Remember that EF uses metadata information when building SQL queries, and in this case I guess the query optimizer decides that this query will never return records (similar to .Where(it => false)) and generates a fake SQL query you see.

简短地-确保始终向EF提供正确的信息.在这种情况下,请将以上内容更改为

Shortly - make sure you always provide the correct information to EF. In this case, change the above to

this.HasOptional(t => t.WfwEventResult)

,您将看到一个不同的(实际)查询并获得正确的结果.

and you'll see a different (real) query and get a correct results.

这篇关于实体框架神秘错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆