与DbQuery的关系

时间:2019-01-17 10:34:15

标签: asp.net-core foreign-keys relationship ef-core-2.2

我有此参与者模型DbSet<Participants>

public class Participant {

    public Guid Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int IsCaptain { get; set; }
    public Guid TeamId { get; set; }

    [ForeignKey("TeamId")]
    public Team Team { get; set; }

}

此ParticipantDataView DbQuery<ParticipantsDataView>

public class ParticipantDataView {

    public Guid Id { get; set; } // = Participant.Id

    public double? FirstWeight { get; set; }
    public double? LastWeight { get; set; }
    public double? WeightLoss => FirstWeight - LastWeight;

    public Participant Participant { get; set; }

}

DbContext:

public class DBContext : DbContext {

    public DBContext(DbContextOptions<DBContext> options) : base(options) {}

    public DbSet<Participant> Participants { get; set; }
    public DbQuery<ParticipantDataView> ParticipantsDataView { get; set; }

}

我的查询:

Participants = await _context.ParticipantsDataView
.Include(p => p.Participant)
.ThenInclude(t => t.Team)
.Where(p => p.Participant.Status == 1 && p.Participant.Team.Status == 1).OrderBy(p => p.WeightLoss)
.AsNoTracking()
.ToListAsync();

错误:

SqlNullValueException: Data is Null.
This method or property cannot be called on Null values.

ParticipantDataView中的每个项目在Participant中都有一个匹配项,所以我不知道为什么会出现此错误?

0 个答案:

没有答案