【发布时间】:2021-01-22 14:29:20
【问题描述】:
我有以下返回工厂汽车列表的方法。
可以,但是顺序不对。
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 模型中,我将其定义如下:
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; }
}
所以我不确定它没有看到它。
有没有办法让它工作?
谢谢!
【问题讨论】:
-
您是否收到错误消息?如果是这样,那是什么错误?或者这是 Intellisense 的问题?
-
给我们所有类的代码。
CountryList,CarList应该足够了 -
@NolanBradshaw 我收到了
CarList doesn't contain a definition for CarEngines -
@NolanBradshaw Visual Studio 中的完整错误是:
Error CS1061 'ICollection<CarList>' does not contain a definition for 'CarEngines' and no accessible extension method 'CarEngines' accepting a first argument of type 'ICollection<CarList>' could be found -
@A_kat 谢谢!我添加了那些
标签: asp.net-core entity-framework-core asp.net-core-3.1