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



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

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上的方法时,请执行null检查(下面的?运算符(,如-

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

以防没有为您的ConstructionSet找到相关的OpaqueConstruction数据。

最新更新