有很多关于具有一对多关系的空指针的问题,但我没有看到这个问题的答案。 我有一个非常简单的案例:
public class Aircraft
{
public int Id { get; set; }
public string Nnumber { get; set; }
public string Owner { get; set; }
public bool Training { get; set; }
public ICollection<Ping> Pings { get; set; } = null!;
}
我做一个查询
var thisAircrafts = from b in context.Aircrafts
where b.Nnumber == newPing.N_Number
select b;
newAircraft = thisAircrafts.FirstOrDefault();
我找到了条目,它在正确的表中,所有值都是正确的。但是,当我向其添加“Ping”时,Pings 变量为空。
newAircraft.Pings.Add(newPing);
newAircraft.Pings 为空。什么时候应该得到初始化。我有另一个非常相似的应用程序,它工作得很好。