实体框架的行,返回null,如果该行的第一列是空 [英] Entity framework returns null for a row if the first column in that row is null

查看:322
本文介绍了实体框架的行,返回null,如果该行的第一列是空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到在我的实体框架模型怪异的行为。我有一个查询,看起来像这样:

I'm seeing a weird behavior in my Entity Framework model. I've got a query that looks like this:

var rows = ( from alarm in context.Alarms
             join temp  in context.ListDetails on alarm.ListDetailId equals  temp.ListDetailId into entries from entry in entries.DefaultIfEmpty()
             join read  in context.Reads       on alarm.ReadId       equals  read.ReadId
             join plate in context.Images      on alarm.ReadId       equals plate.ReadId
             where alarm.IActive == 1 && ! alarm.TransmittedAlarm 
             where  read.IActive == 1 
             where plate.IActive == 1 && plate.ImageTypeId == 2
             select new { alarm, entry, read, plate } ).ToArray();



查询列名返回按字母顺序排列的所有列。事实证明,这个字段是空在结果集的几行。 !当我在调试器中展开排变量,我看到整个行是空

The query returns all columns in alphabetical order by column name. It turns out that this column is NULL for a few rows in the result set. When I expand the rows variable in the debugger, I see that the entire row is null!

编辑:一些澄清

通过第一列,我指的是第一排,即第一列,在选择A,b,C FROM ...,我的意思是A.碰巧的查询实体框架建立返回所有以字母顺序设置连接结果列,并且第一个字母为空,并为空的某些行。

By "first column", I mean the first column of the first row, i.e., in "SELECT A, B, C FROM ...", I mean A. It just happens that the query that Entity Framework builds returns all of the columns in the joined result set in alphabetical order, and the first one alphabetically is nullable and is null for some rows.

有问题的列不是主键;如果它是一个主键,它不能为空。

The column in question is not a primary key; if it were a primary key, it couldn't be null.

当实体框架处理返回的数据行转换为对象,它看起来在每一行中的第一列的值。如果该列为空,它返回null该行,而不是使用对应于该列设置为null属性的对象。

When Entity Framework processes the rows of the returned data into objects, it's looking at the value of the first column in each row. If that column is null, it's returning null for the row, instead of an object with the property that corresponds to that column set to null.

我不相信这个有什么用左外连接做专;它只是碰巧我的查询使用一个。我没有做任何测试不过来验证这一点,所以它只是一个假设。

I don't believe this has anything to do specifically with a left outer join; it just happens that my query uses one. I haven't done any testing to verify this, however, so it's just a supposition.

有没有人见过这个?有没有人有这样一个修复?

Has anybody seen this before? Does anyone have a fix for this?

托尼

推荐答案

我可以证实,我有完全一样的烦恼:当EF形成SQL查询中在结果第一列,它返回null,而不是实体。它的意思如下:

I can confirm that I have exactly same trouble: when SQL query formed by EF has null in first column of result, it return null instead of entity. It means exactly following:

using (var dc = new MyDbContext())
{
    var q = dc.Lands; //Lands is DbSet<Land>
    q = q.Where(criteria); //this leads to SQL query returning null in first column, confirmed by profiler
    var t = q.Single(); //t is now null
}

如果我们采取另一种标准,它不会导致在在第一列中, T 是正常的非 - 实体。

If we take another criteria, which does not result in null in first column, t is normal non-null entity.

在我看来,它是某种模糊的行为/错误的EF

It seems to me that it is some kind of obscure behavior/bug in EF.

更新

几个小时的探测,我发现以下行为后。 EF不会返回具有 NULL 在结果第一列(打破预期的行为)entites的,但它有几个即将把第一选择列表列是什么概念。所以,让我的模型恰恰符合我的数据库状态(即表示所有的 NULLABLE 数据库列和更换后的要求导航我使用的代码第一次,所以是很多地方是明确约数据库 - - 我已经设法与可选)的属性它的工作。

After several hours of probing I've found following behavior. EF does return null for entites having NULL in first column of result (which breaks expected behavior), but it has several notions about what column to put first in the select list. So, after making my model precisely consistent with my database state (which means denoting all NULLABLE database columns and replacing required navigation properties with optional) - I am using Code First, so there're plenty of places to be explicit about database - I've managed to get it to work.

这意味着你应该检查你的模型的存储级别定义(这取决于你用什么模式,设计师为主,或代码为基础的,它将不同的地方)对您的数据库。

It means that you should check store-level definition of your model (depending on what paradigm you use, designer-based, or code-based, it would be different places) against your database.

这篇关于实体框架的行,返回null,如果该行的第一列是空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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