0

实际上,我有一个问题,即我不能为可空字段设置空值而不是默认值。

我无法在 select 语句中使用 EF core 2.1 编写正确的代码,因为我遇到了运行时异常“值不能为空”或者无法使用空传播运算符。

注意:如果重要,则从查询中的子实体获取行。

VotedAt = i.CurrentUserVote == null 
    ? new DateTimeOffset() 
    : i.CurrentUserVote.VotedAt,
CurrentUserVote = i.CurrentUserVote == null 
    ? false 
    : i.CurrentUserVote.IsPositive,

我想写:

VotedAt = i.CurrentUserVote == null 
    ? null 
    : i.CurrentUserVote.VotedAt,
CurrentUserVote = i.CurrentUserVote == null 
    ? null 
    : i.CurrentUserVote.IsPositive,

甚至:

VotedAt = i.CurrentUserVote?.VotedAt,
CurrentUserVote = i.CurrentUserVote?.IsPositive,

社区要求的其他信息: 选定的实体:

public class IssueListItem
{
    public int Id { get; set; }
    public string Title { get; set; }
    public Votes Votes { get; set; }
}

public class Votes
{
    public int Positive { get; set; }
    public int Negative { get; set; }
    public int All { get; set; }
    public bool? CurrentUserVote { get; set; }
    public DateTimeOffset? VotedAt { get; set; }
}

选择:

        return _unitOfWork.Issues.GetQuery()
                .Filter(query.Filter)
                .WithVotes<Issue, IssueVote, int>(currentUserId)
                .Order(query.Sorter)
                .Select(i => new IssueListItem
                {
                    Id = i.Item.Id,
                    Votes = i.Votes,
                    Title = i.Item.Title,
                    //removed some other properties
                })
                .ToListAsync();

哪里WithVotes<Issue, IssueVote, int>(currentUserId)是:

            return q
                .Select(i => new
                {
                    Item = i,
                    VoutesGroups = i.Votes
                        .GroupBy(v => v.IsPositive, v => true, (key, vg) => new { IsPositive = key, Count = vg.Count() }),
                    CurrentUserVote = currentUserId == null ? null : i.Votes.FirstOrDefault(v => v.CreatedById == currentUserId),
                })
                .Select(i => new AssignVotesModel<TEntity, TVote, TId>
                {
                    Item = i.Item,
                    Votes = new Votes
                    {
                        Positive = i.VoutesGroups.Where(vg => vg.IsPositive == true).Sum(vg => vg.Count),
                        Negative = i.VoutesGroups.Where(vg => vg.IsPositive == false).Sum(vg => vg.Count),
                        All = i.VoutesGroups.Sum(vg => vg.Count),
                        VotedAt = i.CurrentUserVote == null ? new DateTimeOffset() : i.CurrentUserVote.VotedAt,
                        CurrentUserVote = i.CurrentUserVote == null ? false : i.CurrentUserVote.IsPositive,
                    }
                });
4

1 回答 1

0

The defined column probably cannot be null in the database, Or in your entity Mapping.

Check that your Entity Model does not have Required attribute. Also check that specified Property is not tagged with Required in modelBuilder configuration.

Lastly, check your Table Column from your RDBMS whether it is set as IS NOT NULL

于 2019-01-25T12:07:42.413 回答