简体   繁体   English

EF6异步包含的实体属性是否为null?

[英]EF6 async included entities properties are null?

Here is the query: 这是查询:

using (var _context = GetCommonDataContext())
    {
        return await _context.AnswerSelectListEntries
            .Include(asle => asle.Optional)
            .OrderBy(asle => asle.DisplayOrder)
            .Select(asle => new AnswerSelectListEntryDTO()
            {
                Id = asle.Id,
                AnswerSelectListId = asle.AnswerSelectListId,
                ShortAnswer = asle.ShortAnswer,
                LongAnswer = asle.LongAnswer,
                CounterpartyGroupId = asle.CounterpartyGroupId,
                Optional = new AnswerDefDTO()
                {
                    Id = asle.Optional.Id,
                    Name = asle.Optional.Name,
                    AnswerType = asle.Optional.AnswerType,
                    MultiplierAnswerType = asle.Optional.MultiplierAnswerType,
                    IntRangeLow = asle.Optional.IntRangeLow,
                    IntRangeHigh = asle.Optional.IntRangeHigh,
                    AnswerSelectListId = asle.Optional.AnswerSelectListId
                },
                AnswerDefId = asle.AnswerDefId,
                PartyType = asle.PartyType,
                DisplayOrder = asle.DisplayOrder
            }).ToListAsync();
    }

I am getting an error (thrown from the subquery) that Id, and AnswerType (which is an enum) cannot be cast to their counterparts (int and enum) because they are null. 我收到一个错误(从子查询抛出),并且Id和AnswerType(这是一个枚举)无法转换为其对应对象(int和enum),因为它们为null。 The issue is that they are non-nullable (and not null in the db). 问题在于它们是不可为空的(在数据库中也不为空)。 Somehow, asle.Optional's properties are being set to null (even though they are non-nullable). 不知何故,asle.Optional的属性被设置为null(即使它们不可为空)。 I am guessing this has something to do with the async but not sure what. 我猜想这与异步有关,但不确定。

Fixed using: 固定使用:

Optional = asle.Optional == null ? null : new AnswerDefDTO()...

My guess would be that, since that thing is optional, and is making some kind of join, most likely a left join in the database, that the columns are coming back as DBNull simply because they weren't actually in the database. 我的猜测是,由于该事件是可选的,并且正在进行某种联接(很可能是数据库中的左联接),因此这些列返回DBNull的原因仅仅是因为它们实际上不在数据库中。 I would advise you to run a SELECT * FROM Optional where ID = whatever asle.Id is. 我建议您运行SELECT * FROM可选,其中ID =无论asle.Id是什么。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM