{"action":"reopened","issue":{"url":"https://api.github.com/repos/aspnet/EntityFramework/issues/1487","labels_url":"https://api.github.com/repos/aspnet/EntityFramework/issues/1487/labels{/name}","comments_url":"https://api.github.com/repos/aspnet/EntityFramework/issues/1487/comments","events_url":"https://api.github.com/repos/aspnet/EntityFramework/issues/1487/events","html_url":"https://github.com/aspnet/EntityFramework/issues/1487","id":55702272,"number":1487,"title":"Include :: 'Specified cast is not valid' thrown for some cases of multi-level include queries","user":{"login":"maumar","id":1591930,"avatar_url":"https://avatars.githubusercontent.com/u/1591930?v=3","gravatar_id":"","url":"https://api.github.com/users/maumar","html_url":"https://github.com/maumar","followers_url":"https://api.github.com/users/maumar/followers","following_url":"https://api.github.com/users/maumar/following{/other_user}","gists_url":"https://api.github.com/users/maumar/gists{/gist_id}","starred_url":"https://api.github.com/users/maumar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/maumar/subscriptions","organizations_url":"https://api.github.com/users/maumar/orgs","repos_url":"https://api.github.com/users/maumar/repos","events_url":"https://api.github.com/users/maumar/events{/privacy}","received_events_url":"https://api.github.com/users/maumar/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/aspnet/EntityFramework/labels/bug","name":"bug","color":"f7c6c7"},{"url":"https://api.github.com/repos/aspnet/EntityFramework/labels/pri0","name":"pri0","color":"e11d21"}],"state":"open","locked":false,"assignee":{"login":"maumar","id":1591930,"avatar_url":"https://avatars.githubusercontent.com/u/1591930?v=3","gravatar_id":"","url":"https://api.github.com/users/maumar","html_url":"https://github.com/maumar","followers_url":"https://api.github.com/users/maumar/followers","following_url":"https://api.github.com/users/maumar/following{/other_user}","gists_url":"https://api.github.com/users/maumar/gists{/gist_id}","starred_url":"https://api.github.com/users/maumar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/maumar/subscriptions","organizations_url":"https://api.github.com/users/maumar/orgs","repos_url":"https://api.github.com/users/maumar/repos","events_url":"https://api.github.com/users/maumar/events{/privacy}","received_events_url":"https://api.github.com/users/maumar/received_events","type":"User","site_admin":false},"milestone":{"url":"https://api.github.com/repos/aspnet/EntityFramework/milestones/7","labels_url":"https://api.github.com/repos/aspnet/EntityFramework/milestones/7/labels","id":666128,"number":7,"title":"7.0.0","description":"","creator":{"login":"rowanmiller","id":4741851,"avatar_url":"https://avatars.githubusercontent.com/u/4741851?v=3","gravatar_id":"","url":"https://api.github.com/users/rowanmiller","html_url":"https://github.com/rowanmiller","followers_url":"https://api.github.com/users/rowanmiller/followers","following_url":"https://api.github.com/users/rowanmiller/following{/other_user}","gists_url":"https://api.github.com/users/rowanmiller/gists{/gist_id}","starred_url":"https://api.github.com/users/rowanmiller/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rowanmiller/subscriptions","organizations_url":"https://api.github.com/users/rowanmiller/orgs","repos_url":"https://api.github.com/users/rowanmiller/repos","events_url":"https://api.github.com/users/rowanmiller/events{/privacy}","received_events_url":"https://api.github.com/users/rowanmiller/received_events","type":"User","site_admin":false},"open_issues":160,"closed_issues":123,"state":"open","created_at":"2014-05-21T19:25:32Z","updated_at":"2015-02-09T22:39:02Z","due_on":null,"closed_at":null},"comments":1,"created_at":"2015-01-28T01:39:49Z","updated_at":"2015-02-09T22:39:02Z","closed_at":null,"body":"Repro:\r\n\r\n```\r\n    public class Level1\r\n    {\r\n        public int Id1 { get; set; }\r\n        public string Name { get; set; }\r\n\r\n        public Level2 OneToOne_Required_PK { get; set; }\r\n        public Level2 OneToOne_Optional_PK { get; set; }\r\n\r\n        public Level2 OneToOne_Required_FK { get; set; }\r\n        public Level2 OneToOne_Optional_FK { get; set; }\r\n\r\n        public ICollection<Level2> OneToMany_Required { get; set; }\r\n        public ICollection<Level2> OneToMany_Optional { get; set; }\r\n\r\n        public ICollection<Level1> OneToMany_Required_Self { get; set; }\r\n        public ICollection<Level1> OneToMany_Optional_Self { get; set; }\r\n        public Level1 OneToMany_Required_Self_Inverse { get; set; }\r\n        public Level1 OneToMany_Optional_Self_Inverse { get; set; }\r\n    }\r\n\r\n    public class Level2\r\n    {\r\n        public int Id2 { get; set; }\r\n        public string Name { get; set; }\r\n\r\n        public int Level1_Required_Id { get; set; }\r\n        public int? Level1_Optional_Id { get; set; }\r\n\r\n        public Level3 OneToOne_Required_PK { get; set; }\r\n        public Level3 OneToOne_Optional_PK { get; set; }\r\n\r\n        public Level3 OneToOne_Required_FK { get; set; }\r\n        public Level3 OneToOne_Optional_FK { get; set; }\r\n\r\n        public ICollection<Level3> OneToMany_Required { get; set; }\r\n        public ICollection<Level3> OneToMany_Optional { get; set; }\r\n\r\n        public Level1 OneToOne_Required_PK_Inverse { get; set; }\r\n        public Level1 OneToOne_Optional_PK_Inverse { get; set; }\r\n        public Level1 OneToOne_Required_FK_Inverse { get; set; }\r\n        public Level1 OneToOne_Optional_FK_Inverse { get; set; }\r\n\r\n        public Level1 OneToMany_Required_Inverse { get; set; }\r\n        public Level1 OneToMany_Optional_Inverse { get; set; }\r\n    }\r\n\r\n    public class Level3\r\n    {\r\n        public int Id3 { get; set; }\r\n        public string Name { get; set; }\r\n\r\n        public int Level2_Required_Id { get; set; }\r\n        public int? Level2_Optional_Id { get; set; }\r\n\r\n        public Level2 OneToOne_Required_PK_Inverse { get; set; }\r\n        public Level2 OneToOne_Optional_PK_Inverse { get; set; }\r\n        public Level2 OneToOne_Required_FK_Inverse { get; set; }\r\n        public Level2 OneToOne_Optional_FK_Inverse { get; set; }\r\n\r\n        public Level2 OneToMany_Required_Inverse { get; set; }\r\n        public Level2 OneToMany_Optional_Inverse { get; set; }\r\n    }\r\n\r\n    public class MyContext : DbContext\r\n    {\r\n        public DbSet<Level1> LevelOne { get; set; }\r\n        public DbSet<Level2> LevelTwo { get; set; }\r\n        public DbSet<Level3> LevelThree { get; set; }\r\n\r\n        protected override void OnModelCreating(ModelBuilder modelBuilder)\r\n        {\r\n            base.OnModelCreating(modelBuilder);\r\n\r\n            modelBuilder.Entity<Level1>().Key(e => e.Id1);\r\n            modelBuilder.Entity<Level2>().Key(e => e.Id2);\r\n            modelBuilder.Entity<Level3>().Key(e => e.Id3);\r\n\r\n            modelBuilder.Entity<Level1>().Property(e => e.Id1).GenerateValueOnAdd(false);\r\n            modelBuilder.Entity<Level2>().Property(e => e.Id2).GenerateValueOnAdd(false);\r\n\r\n            modelBuilder.Entity<Level1>().HasOne(e => e.OneToOne_Required_PK).WithOne(e => e.OneToOne_Required_PK_Inverse).ReferencedKey<Level1>(e => e.Id1).Required(true);\r\n            modelBuilder.Entity<Level1>().HasOne(e => e.OneToOne_Optional_PK).WithOne(e => e.OneToOne_Optional_PK_Inverse).ReferencedKey<Level1>(e => e.Id1).Required(false);\r\n\r\n            modelBuilder.Entity<Level1>().HasOne(e => e.OneToOne_Required_FK).WithOne(e => e.OneToOne_Required_FK_Inverse).ForeignKey<Level2>(e => e.Level1_Required_Id).Required(true);\r\n            modelBuilder.Entity<Level1>().HasOne(e => e.OneToOne_Optional_FK).WithOne(e => e.OneToOne_Optional_FK_Inverse).ForeignKey<Level2>(e => e.Level1_Optional_Id).Required(false);\r\n\r\n            modelBuilder.Entity<Level1>().HasMany(e => e.OneToMany_Required).WithOne(e => e.OneToMany_Required_Inverse).Required(true);\r\n            modelBuilder.Entity<Level1>().HasMany(e => e.OneToMany_Optional).WithOne(e => e.OneToMany_Optional_Inverse).Required(false);\r\n\r\n            modelBuilder.Entity<Level1>().HasMany(e => e.OneToMany_Required_Self).WithOne(e => e.OneToMany_Required_Self_Inverse).Required(true);\r\n            modelBuilder.Entity<Level1>().HasMany(e => e.OneToMany_Optional_Self).WithOne(e => e.OneToMany_Optional_Self_Inverse).Required(false);\r\n\r\n            modelBuilder.Entity<Level2>().Property(e => e.Id2).GenerateValueOnAdd(false);\r\n            modelBuilder.Entity<Level2>().Property(e => e.Id2).GenerateValueOnAdd(false);\r\n\r\n            modelBuilder.Entity<Level2>().HasOne(e => e.OneToOne_Required_PK).WithOne(e => e.OneToOne_Required_PK_Inverse).ReferencedKey<Level2>(e => e.Id2).Required(true);\r\n            modelBuilder.Entity<Level2>().HasOne(e => e.OneToOne_Optional_PK).WithOne(e => e.OneToOne_Optional_PK_Inverse).ReferencedKey<Level2>(e => e.Id2).Required(false);\r\n\r\n            modelBuilder.Entity<Level2>().HasOne(e => e.OneToOne_Required_FK).WithOne(e => e.OneToOne_Required_FK_Inverse).ForeignKey<Level3>(e => e.Level2_Required_Id).Required(true);\r\n            modelBuilder.Entity<Level2>().HasOne(e => e.OneToOne_Optional_FK).WithOne(e => e.OneToOne_Optional_FK_Inverse).ForeignKey<Level3>(e => e.Level2_Optional_Id).Required(false);\r\n\r\n\r\n            modelBuilder.Entity<Level2>().HasOne(e => e.OneToOne_Required_FK).WithOne(e => e.OneToOne_Required_FK_Inverse).ForeignKey<Level3>(e => e.Level2_Required_Id).Required(true);\r\n            modelBuilder.Entity<Level2>().HasOne(e => e.OneToOne_Optional_FK).WithOne(e => e.OneToOne_Optional_FK_Inverse).ForeignKey<Level3>(e => e.Level2_Optional_Id).Required(false);\r\n\r\n            modelBuilder.Entity<Level2>().HasMany(e => e.OneToMany_Required).WithOne(e => e.OneToMany_Required_Inverse).Required(true);\r\n            modelBuilder.Entity<Level2>().HasMany(e => e.OneToMany_Optional).WithOne(e => e.OneToMany_Optional_Inverse).Required(false);\r\n\r\n            modelBuilder.Entity<Level3>().Property(e => e.Id3).GenerateValueOnAdd(false);\r\n            modelBuilder.Entity<Level3>().Property(e => e.Id3).GenerateValueOnAdd(false);\r\n        }\r\n\r\n        protected override void OnConfiguring(DbContextOptions options)\r\n        {\r\n            options.UseSqlServer(new SqlConnectionStringBuilder { DataSource = \".\", InitialCatalog = \"Repro1487\", MultipleActiveResultSets = true, IntegratedSecurity = true }.ToString());\r\n        }\r\n    }\r\n\r\n    class Program\r\n    {\r\n        static void Main(string[] args)\r\n        {\r\n\r\n            using (var ctx = new MyContext())\r\n            {\r\n                ctx.Database.EnsureDeleted();\r\n                ctx.Database.EnsureCreated();\r\n\r\n                var l1_01 = new Level1 { Id1 = 1, Name = \"L1 01\", };\r\n                var l1_02 = new Level1 { Id1 = 2, Name = \"L1 02\", };\r\n                var l1_03 = new Level1 { Id1 = 3, Name = \"L1 03\", };\r\n                var l1_04 = new Level1 { Id1 = 4, Name = \"L1 04\", };\r\n\r\n                var l2_01 = new Level2 { Id2 = 1, Name = \"L2 01\", };\r\n                var l2_02 = new Level2 { Id2 = 2, Name = \"L2 02\", };\r\n                var l2_03 = new Level2 { Id2 = 3, Name = \"L2 03\", };\r\n                var l2_04 = new Level2 { Id2 = 4, Name = \"L2 04\", };\r\n\r\n                var l3_01 = new Level3 { Id3 = 1, Name = \"L3 01\", };\r\n                var l3_02 = new Level3 { Id3 = 2, Name = \"L3 02\", };\r\n                var l3_03 = new Level3 { Id3 = 3, Name = \"L3 03\", };\r\n                var l3_04 = new Level3 { Id3 = 4, Name = \"L3 04\", };\r\n\r\n                var l1s = new[] { l1_01, l1_02, l1_03, l1_04, }.ToArray();\r\n                var l2s = new[] { l2_01, l2_02, l2_03, l2_04, }.ToArray();\r\n                var l3s = new[] { l3_01, l3_02, l3_03, l3_04, }.ToArray();\r\n\r\n                ctx.LevelOne.Add(l1s);\r\n                ctx.LevelTwo.Add(l2s);\r\n                ctx.LevelThree.Add(l3s);\r\n\r\n                l1s[0].OneToOne_Required_PK = l2s[0];\r\n                l1s[1].OneToOne_Required_PK = l2s[1];\r\n                l1s[2].OneToOne_Required_PK = l2s[2];\r\n                l1s[3].OneToOne_Required_PK = l2s[3];\r\n\r\n                l1s[0].OneToOne_Required_FK = l2s[3];\r\n                l1s[1].OneToOne_Required_FK = l2s[2];\r\n                l1s[2].OneToOne_Required_FK = l2s[1];\r\n                l1s[3].OneToOne_Required_FK = l2s[0];\r\n\r\n                l1s[0].OneToMany_Required = new List<Level2> { l2s[0], l2s[1], l2s[2], l2s[3], };\r\n\r\n                l1s[0].OneToMany_Required_Self = new List<Level1> { l1s[0], l1s[1] };\r\n                l1s[1].OneToMany_Required_Self = new List<Level1> { l1s[2] };\r\n                l1s[2].OneToMany_Required_Self = new List<Level1> { l1s[3] };\r\n\r\n                l2s[0].OneToOne_Required_PK = l3s[0];\r\n                l2s[1].OneToOne_Required_PK = l3s[1];\r\n                l2s[2].OneToOne_Required_PK = l3s[2];\r\n                l2s[3].OneToOne_Required_PK = l3s[3];\r\n\r\n                l2s[0].OneToOne_Required_FK = l3s[3];\r\n                l2s[1].OneToOne_Required_FK = l3s[2];\r\n                l2s[2].OneToOne_Required_FK = l3s[1];\r\n                l2s[3].OneToOne_Required_FK = l3s[0];\r\n\r\n                l2s[0].OneToMany_Required = new List<Level3> { l3s[0], l3s[1], l3s[2], l3s[3], };\r\n\r\n                ctx.SaveChanges();\r\n\r\n                l1s[0].OneToOne_Optional_PK = l2s[0];\r\n                l1s[2].OneToOne_Optional_PK = l2s[2];\r\n\r\n                l1s[1].OneToOne_Optional_FK = l2s[2];\r\n                l1s[3].OneToOne_Optional_FK = l2s[0];\r\n\r\n                l1s[0].OneToMany_Optional = new List<Level2> { l2s[1], l2s[3], };\r\n\r\n                l1s[1].OneToMany_Optional_Self = new List<Level1> { l1s[0] };\r\n                l1s[3].OneToMany_Optional_Self = new List<Level1> { l1s[2] };\r\n\r\n                l2s[0].OneToOne_Optional_PK = l3s[0];\r\n                l2s[2].OneToOne_Optional_PK = l3s[2];\r\n\r\n                l2s[1].OneToOne_Optional_FK = l3s[2];\r\n                l2s[3].OneToOne_Optional_FK = l3s[0];\r\n\r\n                l2s[0].OneToMany_Optional = new List<Level3> { l3s[1], l3s[3], };\r\n\r\n                ctx.SaveChanges();\r\n            }\r\n\r\n\r\n            using (var ctx = new MyContext())\r\n            {\r\n                ctx.LevelOne\r\n                    .Include(e => e.OneToMany_Optional)\r\n                    .ThenInclude(e => e.OneToMany_Optional_Inverse)\r\n                    .ThenInclude(e => e.OneToOne_Optional_PK)\r\n                    .ThenInclude(e => e.OneToOne_Optional_FK).ToList();\r\n\r\n                ctx.LevelOne\r\n                    .Include(e => e.OneToMany_Optional)\r\n                    .ThenInclude(e => e.OneToMany_Optional_Inverse)\r\n                    .ThenInclude(e => e.OneToOne_Optional_PK)\r\n                    .ThenInclude(e => e.OneToOne_Optional_FK_Inverse).ToList();\r\n            }\r\n        }\r\n    }\r\n```\r\n\r\nthrows:\r\n\r\nUnhandled Exception: System.InvalidCastException: Specified cast is not valid.\r\n   at Microsoft.Data.Entity.Metadata.ObjectArrayValueReader.ReadValue[T](Int32 i\r\nndex) in D:\\k\\EntityFramework\\src\\EntityFramework.Core\\Metadata\\ObjectArrayValue\r\nReader.cs:line 27\r\n   at Microsoft.Data.Entity.Relational.Query.OffsetValueReaderDecorator.ReadValu\r\ne[T](Int32 index) in D:\\k\\EntityFramework\\src\\EntityFramework.Relational\\Query\\O\r\nffsetValueReaderDecorator.cs:line 30\r\n   at Microsoft.Data.Entity.ChangeTracking.SimpleEntityKeyFactory`1.Create(IEnti\r\ntyType entityType, IReadOnlyList`1 properties, IValueReader valueReader) in D:\\k\r\n\\EntityFramework\\src\\EntityFramework.Core\\ChangeTracking\\SimpleEntityKeyFactory.\r\ncs:line 18\r\n   at Microsoft.Data.Entity.Query.QueryBuffer.IncludeCore(Object entity, INaviga\r\ntion navigation, EntityKey& primaryKey, List`1& bufferedEntities, Func`2& relate\r\ndKeyFactory) in D:\\k\\EntityFramework\\src\\EntityFramework.Core\\Query\\QueryBuffer.\r\ncs:line 294\r\n   at Microsoft.Data.Entity.Query.QueryBuffer.Include(Object entity, IReadOnlyLi\r\nst`1 navigationPath, IReadOnlyList`1 relatedValueReaders, Int32 currentNavigatio\r\nnIndex) in D:\\k\\EntityFramework\\src\\EntityFramework.Core\\Query\\QueryBuffer.cs:li\r\nne 176\r\n   at Microsoft.Data.Entity.Query.QueryBuffer.<>c__DisplayClass14_0.<Include>b__\r\n0(IValueReader valueReader) in D:\\k\\EntityFramework\\src\\EntityFramework.Core\\Que\r\nry\\QueryBuffer.cs:line 193\r\n   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()\r\n   at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()\r\n   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)\r\n   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)\r\n   at Microsoft.Data.Entity.Query.QueryBuffer.Include(Object entity, IReadOnlyLi\r\nst`1 navigationPath, IReadOnlyList`1 relatedValueReaders, Int32 currentNavigatio\r\nnIndex) in D:\\k\\EntityFramework\\src\\EntityFramework.Core\\Query\\QueryBuffer.cs:li\r\nne 184\r\n   at Microsoft.Data.Entity.Query.QueryBuffer.<>c__DisplayClass14_0.<Include>b__\r\n0(IValueReader valueReader) in D:\\k\\EntityFramework\\src\\EntityFramework.Core\\Que\r\nry\\QueryBuffer.cs:line 193\r\n   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()\r\n   at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()\r\n   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)\r\n   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)\r\n   at Microsoft.Data.Entity.Query.QueryBuffer.Include(Object entity, IReadOnlyLi\r\nst`1 navigationPath, IReadOnlyList`1 relatedValueReaders, Int32 currentNavigatio\r\nnIndex) in D:\\k\\EntityFramework\\src\\EntityFramework.Core\\Query\\QueryBuffer.cs:li\r\nne 184\r\n   at Microsoft.Data.Entity.Query.QueryBuffer.<>c__DisplayClass14_0.<Include>b__\r\n0(IValueReader valueReader) in D:\\k\\EntityFramework\\src\\EntityFramework.Core\\Que\r\nry\\QueryBuffer.cs:line 193\r\n   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()\r\n   at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()\r\n   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)\r\n   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)\r\n   at Microsoft.Data.Entity.Query.QueryBuffer.Include(Object entity, IReadOnlyLi\r\nst`1 navigationPath, IReadOnlyList`1 relatedValueReaders, Int32 currentNavigatio\r\nnIndex) in D:\\k\\EntityFramework\\src\\EntityFramework.Core\\Query\\QueryBuffer.cs:li\r\nne 184\r\n   at Microsoft.Data.Entity.Query.QueryBuffer.Include(Object entity, IReadOnlyLi\r\nst`1 navigationPath, IReadOnlyList`1 relatedValueReaders) in D:\\k\\EntityFramewor\r\nk\\src\\EntityFramework.Core\\Query\\QueryBuffer.cs:line 154\r\n   at Microsoft.Data.Entity.Relational.Query.QueryMethodProvider.<>c__DisplayCla\r\nss11_0`1.<_Include>b__2(T qss) in D:\\k\\EntityFramework\\src\\EntityFramework.Relat\r\nional\\Query\\QueryMethodProvider.cs:line 92\r\n   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()\r\n   at Microsoft.Data.Entity.Relational.Utilities.EnumerableExtensions.<Finally>d\r\n__0`1.MoveNext() in D:\\k\\EntityFramework\\src\\EntityFramework.Relational\\Utilitie\r\ns\\EnumerableExtensions.cs:line 15\r\n   at System.Linq.Enumerable.<SelectManyIterator>d__8`2.MoveNext()\r\n   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()\r\n   at Microsoft.Data.Entity.Query.EntityQueryExecutor.EnumerableExceptionInterce\r\nptor`1.EnumeratorExceptionInterceptor.MoveNext() in D:\\k\\EntityFramework\\src\\Ent\r\nityFramework.Core\\Query\\EntityQueryExecutor.cs:line 179\r\n   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)\r\n   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)\r\n   at Repro1487.Program.Main(String[] args) in D:\\Projects\\Repro1487\\Repro1487\\P\r\nrogram.cs:line 221\r\n"}}