{"action":"opened","issue":{"url":"https://api.github.com/repos/aspnet/EntityFramework/issues/4093","labels_url":"https://api.github.com/repos/aspnet/EntityFramework/issues/4093/labels{/name}","comments_url":"https://api.github.com/repos/aspnet/EntityFramework/issues/4093/comments","events_url":"https://api.github.com/repos/aspnet/EntityFramework/issues/4093/events","html_url":"https://github.com/aspnet/EntityFramework/issues/4093","id":122305534,"number":4093,"title":"Many-to-Many Fluent API Invalid Column Name","user":{"login":"prasannapattam","id":4832935,"avatar_url":"https://avatars.githubusercontent.com/u/4832935?v=3","gravatar_id":"","url":"https://api.github.com/users/prasannapattam","html_url":"https://github.com/prasannapattam","followers_url":"https://api.github.com/users/prasannapattam/followers","following_url":"https://api.github.com/users/prasannapattam/following{/other_user}","gists_url":"https://api.github.com/users/prasannapattam/gists{/gist_id}","starred_url":"https://api.github.com/users/prasannapattam/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/prasannapattam/subscriptions","organizations_url":"https://api.github.com/users/prasannapattam/orgs","repos_url":"https://api.github.com/users/prasannapattam/repos","events_url":"https://api.github.com/users/prasannapattam/events{/privacy}","received_events_url":"https://api.github.com/users/prasannapattam/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-12-15T16:06:41Z","updated_at":"2015-12-15T16:06:41Z","closed_at":null,"body":"\r\n\r\nThis code was broken from Beta 8 to RC1\r\nThis issue seems similar to #1288.\r\nWhen using ThenInclude invalid column name is generated in the query (Exception: Invalid column name 'IdentityPageEntityPageId'.)\r\n\r\nI have three models, IdentityPage, IdentityClaim and IdentityPageClaim. Here are the stripped version of these classes\r\n\r\n```c#\r\n    [Table(\"IdentityPage\")]\r\n    public class IdentityPageEntity\r\n    {\r\n        [Key]\r\n        public int PageId { get; set; }\r\n        public string Text { get; set; }\r\n        public List<IdentityPageClaimEntity> Claims { get; set; }\r\n    }\r\n\r\n    [Table(\"IdentityClaim\")]\r\n    public class IdentityClaimEntity\r\n    {\r\n        [Key]\r\n        public int Id { get; set; }\r\n        public string ClaimType { get; set; }\r\n        public string ClaimValue { get; set; }\r\n        public List<IdentityPageClaimEntity> Pages { get; set; }\r\n    }\r\n\r\n    [Table(\"IdentityPageClaim\")]\r\n    public class IdentityPageClaimEntity\r\n    {\r\n        [Key]\r\n        public int Id { get; set; }\r\n        public int PageId { get; set; }\r\n        public int ClaimId { get; set; }\r\n        [ForeignKey(\"ClaimId\")]\r\n        public IdentityClaimEntity Claim { get; set; }\r\n        [ForeignKey(\"PageId\")]\r\n        public IdentityPageEntity Page { get; set; }\r\n    }\r\n```\r\nHere is the query using ThenInclude\r\n```c#\r\nvar query = from page in dbContext.IdentityPages.Include(c => c.Claims).ThenInclude(a => a.Claim) select page;\r\nreturn query.ToList();\r\n```\r\nThe above linq generated the following select statement\r\n```sql\r\nSELECT [i].[Id], [i].[ClaimId], [i].[PageId], [i].[PrimaryClaimInd], [i0].[Id], [i0].[ClaimType], [i0].[ClaimValue], [i0].[Description], \r\n[i0].[IdentityPageEntityPageId] -- this is the invalid column it generated\r\nFROM [IdentityPageClaim] AS [i]\r\nINNER JOIN (\r\n    SELECT DISTINCT [c].[PageId]\r\n    FROM [IdentityPage] AS [c]\r\n) AS [c] ON [i].[PageId] = [c].[PageId]\r\nINNER JOIN [IdentityClaim] AS [i0] ON [i].[ClaimId] = [i0].[Id]\r\nORDER BY [c].[PageId]\r\n```\r\nI also tried the below explicit configuration, but still getting the same error\r\n```c#\r\nbuilder.Entity<IdentityPageClaimEntity>(entity =>\r\n{\r\n    entity.HasKey(e => e.Id);\r\n    entity.HasOne(d => d.Claim).WithMany(p => p.Pages).HasForeignKey(d => d.ClaimId);\r\n    entity.HasOne(d => d.Page).WithMany(p => p.Claims).HasForeignKey(d => d.PageId);\r\n});\r\n\r\nbuilder.Entity<IdentityPageEntity>(entity =>\r\n{\r\n    entity.HasKey(e => e.PageId);\r\n    entity.HasMany(d => d.Claims).WithOne(p => p.Page).HasForeignKey(d => d.PageId);\r\n});\r\n\r\nbuilder.Entity<IdentityClaimEntity>(entity =>\r\n{\r\n    entity.HasKey(e => e.Id);\r\n    entity.HasMany(d => d.Pages).WithOne(p => p.Claim).HasForeignKey(d => d.ClaimId);\r\n});\r\n```\r\n\r\n"}}