【发布时间】:2015-12-02 14:59:02
【问题描述】:
我偶然发现了这个;我将导航属性定义为 Stack;虽然当我尝试使用Include 查询实体时,DB 关系创建得很好,但它说A specified Include path is not valid. The EntityType 'BreakAway.Destination' does not declare a navigation property with the name 'Lodgings'.
根据这个post 的公认答案,只要导航属性类型实现ICollection 应该没问题。我只是仔细检查Stack<T> 确实实现了ICollection
实体是:
public class Destination
{
public Destination()
{
Lodgings = new Stack<Lodging>();
}
public string Name { get; set; }
public string Country { get; set; }
public int DestinationId { get; set; }
public string Description { get; set; }
public byte[] Photos { get; set; }
public virtual Stack<Lodging> Lodgings { get; set; }
}
public sealed class Lodging
{
public int LodgingId { get; set; }
public string Name { get; set; }
public string Owner { get; set; }
public bool IsResort { get; set; }
public decimal MilesFromNearestAirport { get; set; }
public Destination Destination { get; set; }
public int DestinationId { get; set; }
}
简单的测试查询:
private static void QueryDestination()
{
using (var context = new Context())
{
var dest = context.Destinations.Include(d => d.Lodgings).First();
Console.WriteLine("Destination Name: {0}",dest.Name);
Console.WriteLine("Lodging Name " + dest.Lodgings.First().Name);
}
}
【问题讨论】:
-
您是否尝试将定义更改为
ICollection像这样public virtual ICollection<Lodging> Lodgings? -
Stack<T>没有实现ICollection<T> -
@YacoubMassad 所说的,
Stack<T>实现了ICollection,而不是ICollection<T>。 -
即使它起作用了也毫无意义,因为数据库行是无序的。
标签: c# entity-framework ef-code-first navigation-properties