为什么 Stack<T> 在 EF 中不能作为导航 属性

Why does Stack<T> not work as a navigation propery in EF

我偶然发现了这个;我定义了一个导航 属性 as Stack;尽管当我尝试使用 Include 查询实体时,数据库关系创建得很好,它说 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<T> 而不是 ICollection 的类型。 Stack<T> 仅实现 ICollection.

引用自this reference

A navigation property that represents the "many" end of a relationship must return a type that implements ICollection, where T is the type of the object at the other end of the relationship.

参考中的 ICollection 有一个指向 ICollection<T>.

的 MSDN 参考的 link