繁体   English   中英

子关系在父 object 中不可用

[英]child relationship not available in parent object

我有以下返回工厂汽车列表的方法。

它有效,但顺序错误。

CarEngines 可以有一个 orderId,我想通过它来订购。

看看这里的其他答案,我发现你不能在查询中进行排序,你必须在之后进行。

问题是,我无法访问CarEngines ,如下所示:

public async Task<ActionResult<CountryList>> GetCountryCarObject(Guid countryID)
{
    var factoryCars = await _context.CountryList
        .Include(n => n.CarList).ThenInclude(l => l.CarEngines)
        .Include(n => n.CarList).ThenInclude(l => l.CarOptions)
        .SingleOrDefaultAsync(c => c.CountryId == countryID);

    factoryCars.CarList.CarEngines  <== CarEngines doesn't show up in CarList object

    return factoryCars;
}

它告诉我 CarList 不包含 CarEngines 的定义。

但它在我的 CarList model 中,我是这样定义的:

public CarList()
{
    CarEngines = new HashSet<CarEngines>();
}

public virtual ICollection<CarEngines> CarEngines { get; set; }

这是两个模型:

public partial class CarList
{
    public CarList()
    {
        CarEngines = new HashSet<CarEngines>();
        CarOptions = new HashSet<CarOptions>();
    }

    public string CarId { get; set; }
    public string CarMake { get; set; }
    public string CarModel { get; set; }
    public Guid? CarCountryId { get; set; }

    public virtual ICollection<CarEngines> CarEngines { get; set; }
    public ICollection<CarOptions> CarOptions { get; set; }
}


public partial class CountryList
{
    public CountryList()
    {
        CarList = new HashSet<CarList>();
    }

    [Key]
    public Guid CountryId { get; set; }
    public string CountryName { get; set; }
    public string CountryLocation { get; set; }
    public string CountryDesc { get; set; }

    public virtual ICollection<CarList> CarList { get; set; }

}

所以我不确定它没有看到它。

有没有办法让它工作?

谢谢!

好吧,事实上有一个叫做CarList但不是 List 的东西非常令人困惑但继续......

问题是CarList是一个列表。 所以使用类似factoryCars.CarList.Select( x=>x.CarEngines)的东西。 还要将其重命名为var country而不是var factoryCars ,因为您返回的是一个国家而不是汽车列表。

同时重命名您的变量和类,这种混淆可能是由此引起的。 例如,您可以将其重命名为ICollection<Car> Cars <CarList> CarList 而不是ICollection<CarList> CarList ,因此现在从名称中您可以轻松地理解有多辆汽车(因此它是一个集合),其中包括 object Car

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM