访问其他实体内部的实体方法获取对象引用错误

accessing entity method inside other entity getting object reference error

我有实体 类 和里面的方法,如下所示

public class OpaqueConstruction :AEIMasterBase
{
    public string Name { get; set; }
    [Column(TypeName = "jsonb")]
    public List<OpaqueMaterial> Layers { get; set; }

    public Construction AddToOsm(Model model)
    {
        if (model is null)
        {
            throw new ArgumentNullException(nameof(model));
        }

        // code 
        construction.setLayers(materials);
        return construction;
    }
    public OpaqueConstruction() { }
}

public class ConstructionSet : AEIMaster
{
    [ForeignKey("ExteriorWall"), GraphQLIgnore]
    public Guid? ExteriorWallId { get; set; }
    public virtual OpaqueConstruction ExteriorWall { get; set; }

    public void AddToOsm(Model model)
    {
        if (model is null)
        {
            throw new ArgumentNullException(nameof(model));
        }

        using var constructionSet = new DefaultConstructionSet(model);
        using var exteriorSurfaceConstructions = new DefaultSurfaceConstructions(model);

        using var exteriorWall = this.ExteriorWall.AddToOsm(model); // getting error at here object reference set exception
        exteriorSurfaceConstructions.setWallConstruction(exteriorWall);

    }
    public ConstructionSet () { }
}

我正在尝试通过导航属性从其他实体访问写在一个实体中的方法 this.ExteriorWall.AddToOsm(model) 并获取对象引用错误,但无法弄清楚这些 类 是实际实体,我正在将 EF 核心与 .net 核心一起使用

任何人都可以让我知道我在上面的代码中做错了什么,在此先感谢!!

每当您查询 ConstructionSet 时,请将 ExteriorWall 包含在其中,例如 -

var constructionSet = context.ConstructionSets.Include(p=> p.ExteriorWall).FirstOrDefault();

并且,当您在 ExteriorWall 上调用方法时,执行空检查(下面的 ? 运算符),例如 -

var exteriorWall = this.ExteriorWall?.AddToOsm(model);

如果找不到与您的 ConstructionSet 相关的 OpaqueConstruction 数据。