{"action":"created","issue":{"url":"https://api.github.com/repos/aspnet/EntityFramework/issues/3180","labels_url":"https://api.github.com/repos/aspnet/EntityFramework/issues/3180/labels{/name}","comments_url":"https://api.github.com/repos/aspnet/EntityFramework/issues/3180/comments","events_url":"https://api.github.com/repos/aspnet/EntityFramework/issues/3180/events","html_url":"https://github.com/aspnet/EntityFramework/issues/3180","id":107579224,"number":3180,"title":"A column has been specified more than once in the order by list. Columns in the order by list must be unique.","user":{"login":"Venomed","id":808099,"avatar_url":"https://avatars.githubusercontent.com/u/808099?v=3","gravatar_id":"","url":"https://api.github.com/users/Venomed","html_url":"https://github.com/Venomed","followers_url":"https://api.github.com/users/Venomed/followers","following_url":"https://api.github.com/users/Venomed/following{/other_user}","gists_url":"https://api.github.com/users/Venomed/gists{/gist_id}","starred_url":"https://api.github.com/users/Venomed/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Venomed/subscriptions","organizations_url":"https://api.github.com/users/Venomed/orgs","repos_url":"https://api.github.com/users/Venomed/repos","events_url":"https://api.github.com/users/Venomed/events{/privacy}","received_events_url":"https://api.github.com/users/Venomed/received_events","type":"User","site_admin":false},"labels":[{"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/8","html_url":"https://github.com/aspnet/EntityFramework/milestones/Investigation","labels_url":"https://api.github.com/repos/aspnet/EntityFramework/milestones/8/labels","id":666133,"number":8,"title":"Investigation","description":"This release is used to track work items that need to be investigated to determine if they are legitimate issues and whether they should be fixed in the current release.\r\n<!--\r\n@huboard:{\"order\":-2}\r\n-->","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":7,"closed_issues":32,"state":"open","created_at":"2014-05-21T19:27:44Z","updated_at":"2015-10-05T17:30:53Z","due_on":null,"closed_at":null},"comments":18,"created_at":"2015-09-21T19:12:33Z","updated_at":"2015-10-12T11:25:33Z","closed_at":null,"body":"I'm running the following query to test out relation configuration in context and get the above error:\r\n\r\n```csharp\r\ncontext.Snapshots.Where(x => x.CorrelationId == correlationId)\r\n        .Include(x => x.OrganisationSnapshot)\r\n            .ThenInclude(x => x.Organisation)\r\n        .Include(x => x.OrganisationSnapshot)\r\n            .ThenInclude(x => x.OrganisationSnapshotSubscriptions)\r\n                .ThenInclude(x => x.Subscription)\r\n                    .ThenInclude(x => x.Application)\r\n        .Include(x => x.UserSnapshots)\r\n            .ThenInclude(x => x.User)\r\n        .Include(x => x.UserSnapshots)\r\n            .ThenInclude(x => x.UserSnapshotSubscriptions)\r\n                .ThenInclude(x => x.Subscription)\r\n                    .ThenInclude(x => x.Application)\r\n        .Take(10);\r\n```\r\n\r\nif I break it down to:\r\n\r\n```csharp\r\ncontext.Snapshots.Where(x => x.CorrelationId == correlationId)\r\n        .Include(x => x.OrganisationSnapshot)\r\n            .ThenInclude(x => x.Organisation)\r\n        .Include(x => x.OrganisationSnapshot)\r\n            .ThenInclude(x => x.OrganisationSnapshotSubscriptions)\r\n                .ThenInclude(x => x.Subscription)\r\n                    .ThenInclude(x => x.Application)\r\n        .Take(10);\r\n```\r\n\r\nor \r\n\r\n```csharp\r\ncontext.Snapshots.Where(x => x.CorrelationId == correlationId)\r\n        .Include(x => x.OrganisationSnapshot)\r\n            .ThenInclude(x => x.Organisation)\r\n        .Include(x => x.OrganisationSnapshot)\r\n            .ThenInclude(x => x.OrganisationSnapshotSubscriptions)\r\n                .ThenInclude(x => x.Subscription)\r\n                    .ThenInclude(x => x.Application)\r\n        .Take(10);\r\n```\r\nthen it works fine.\r\n\r\nProfiler shows that SQL is indeed messed up:\r\n\r\n```sql\r\nexec sp_executesql N'SELECT TOP(10) [x].[Id], [other column names snipped for brevity]\r\nFROM [Snapshots] AS [x]\r\nLEFT JOIN [OrganisationSnapshots] AS [o] ON [o].[Id] = [x].[Id]\r\nLEFT JOIN [Organisations] AS [o0] ON [o].[OrganisationId] = [o0].[Id]\r\nLEFT JOIN [OrganisationSnapshots] AS [o1] ON [o1].[Id] = [x].[Id]\r\nWHERE [x].[CorrelationId] = @__correlationId_0\r\nORDER BY [o1].[Id], [o1].[Id]',N'@__correlationId_0 uniqueidentifier',@__correlationId_0='xxx'\r\n\r\n         ^^^^^^^^^^^^^^^^^^^^ \r\n```"},"comment":{"url":"https://api.github.com/repos/aspnet/EntityFramework/issues/comments/147369333","html_url":"https://github.com/aspnet/EntityFramework/issues/3180#issuecomment-147369333","issue_url":"https://api.github.com/repos/aspnet/EntityFramework/issues/3180","id":147369333,"user":{"login":"Venomed","id":808099,"avatar_url":"https://avatars.githubusercontent.com/u/808099?v=3","gravatar_id":"","url":"https://api.github.com/users/Venomed","html_url":"https://github.com/Venomed","followers_url":"https://api.github.com/users/Venomed/followers","following_url":"https://api.github.com/users/Venomed/following{/other_user}","gists_url":"https://api.github.com/users/Venomed/gists{/gist_id}","starred_url":"https://api.github.com/users/Venomed/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Venomed/subscriptions","organizations_url":"https://api.github.com/users/Venomed/orgs","repos_url":"https://api.github.com/users/Venomed/repos","events_url":"https://api.github.com/users/Venomed/events{/privacy}","received_events_url":"https://api.github.com/users/Venomed/received_events","type":"User","site_admin":false},"created_at":"2015-10-12T11:25:33Z","updated_at":"2015-10-12T11:25:33Z","body":"@maumar I think I've isolated the problem using your repro code. You're using nullable FKs and I'm not. Otherwise your model code is pretty much identical to mine. I'm targetting .net 4.6 full. I had to comment out your group by query as it was throwing \"An item with the same key has already been added.\". Here's a complete repro code using my own model entities and indeed reproduces the problem on RC1-15945:\r\n\r\n```cs\r\n\r\nnamespace Repro3180\r\n{\r\n    using System;\r\n    using System.Linq;\r\n    using System.Collections.Generic;\r\n    using System.Data.SqlClient;\r\n    using Microsoft.Data.Entity;\r\n\r\n    public class Application\r\n    {\r\n        public int Id { get; set; }\r\n\r\n        public string Name { get; set; }\r\n\r\n        public int AppId { get; set; }\r\n\r\n        public string Checksum { get; set; }\r\n\r\n        public virtual List<Subscription> Subscriptions { get; set; }\r\n    }\r\n\r\n    public class Organisation\r\n    {\r\n        public int Id { get; set; }\r\n\r\n        public string Checksum { get; set; }\r\n\r\n        public virtual List<OrganisationSnapshot> OrganisationSnapshots { get; set; }\r\n    }\r\n\r\n    public class OrganisationSnapshot\r\n    {\r\n        public int Id { get; set; }\r\n\r\n        public int OrganisationId { get; set; }\r\n\r\n        public virtual Organisation Organisation { get; set; }\r\n\r\n        public virtual Snapshot Snapshot { get; set; }\r\n\r\n        public virtual List<OrganisationSnapshotSubscription> OrganisationSnapshotSubscriptions { get; set; }\r\n    }\r\n\r\n    public class OrganisationSnapshotSubscription\r\n    {\r\n        public int Id { get; set; }\r\n\r\n        public int OrganisationSnapshotId { get; set; }\r\n\r\n        public int SubscriptionId { get; set; }\r\n\r\n        public virtual OrganisationSnapshot OrganisationSnapshot { get; set; }\r\n\r\n        public virtual Subscription Subscription { get; set; }\r\n    }\r\n\r\n    public class Snapshot\r\n    {\r\n        public int Id { get; set; }\r\n\r\n        public Guid CorrelationId { get; set; }\r\n\r\n        public DateTime GenerationTime { get; set; }\r\n\r\n        public virtual OrganisationSnapshot OrganisationSnapshot { get; set; }\r\n\r\n        public virtual List<UserSnapshot> UserSnapshots { get; set; }\r\n        \r\n    }\r\n\r\n    public class Subscription\r\n    {\r\n        public int Id { get; set; }\r\n\r\n        public string Checksum { get; set; }\r\n\r\n        public virtual Application Application { get; set; }\r\n\r\n        public virtual List<OrganisationSnapshotSubscription> OrganisationSnapshotSubscriptions { get; set; }\r\n\r\n        public virtual List<UserSnapshotSubscription> UserSnapshotSubscriptions { get; set; }\r\n    }\r\n\r\n    public class User\r\n    {\r\n        public int Id { get; set; }\r\n\r\n        public DateTime CreatedOn { get; set; }\r\n\r\n        public string Checksum { get; set; }\r\n\r\n        public virtual List<UserSnapshot> UserSnapshots { get; set; }\r\n    }\r\n\r\n\r\n    public class UserSnapshot\r\n    {\r\n        public int Id { get; set; }\r\n\r\n        public int UserId { get; set; }\r\n\r\n        public int SnapshotId { get; set; }\r\n\r\n        public virtual Snapshot Snapshot { get; set; }\r\n\r\n        public virtual User User { get; set; }\r\n\r\n        public virtual List<UserSnapshotSubscription> UserSnapshotSubscriptions { get; set; }\r\n    }\r\n\r\n    public class UserSnapshotSubscription\r\n    {\r\n        public int Id { get; set; }\r\n\r\n        public int UserSnapshotId { get; set; }\r\n\r\n        public int SubscriptionId { get; set; }\r\n\r\n        public virtual Subscription Subscription { get; set; }\r\n\r\n        public virtual UserSnapshot UserSnapshot { get; set; }\r\n    }\r\n\r\n    public class MyContext : DbContext\r\n    {\r\n        public virtual DbSet<Application> Applications { get; set; }\r\n\r\n        public virtual DbSet<Organisation> Organisations { get; set; }\r\n\r\n        public virtual DbSet<OrganisationSnapshot> OrganisationSnapshots { get; set; }\r\n\r\n        public virtual DbSet<OrganisationSnapshotSubscription> OrganisationSnapshotSubscriptions { get; set; }\r\n\r\n        public virtual DbSet<Snapshot> Snapshots { get; set; }\r\n\r\n        public virtual DbSet<Subscription> Subscriptions { get; set; }\r\n\r\n        public virtual DbSet<User> Users { get; set; }\r\n\r\n        public virtual DbSet<UserSnapshot> UserSnapshots { get; set; }\r\n\r\n        public virtual DbSet<UserSnapshotSubscription> UserSnapshotSubscriptions { get; set; }\r\n\r\n        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)\r\n        {\r\n            optionsBuilder.UseSqlServer(new SqlConnectionStringBuilder\r\n            {\r\n                DataSource = \".\",\r\n                InitialCatalog = \"Repro3180\",\r\n                MultipleActiveResultSets = false,\r\n                IntegratedSecurity = true,\r\n                ApplicationIntent = ApplicationIntent.ReadOnly\r\n            }.ToString());\r\n        }\r\n\r\n        protected override void OnModelCreating(ModelBuilder modelBuilder)\r\n        {\r\n            base.OnModelCreating(modelBuilder);\r\n\r\n            modelBuilder.Entity<Snapshot>().ToTable(\"Snapshots\");\r\n            modelBuilder.Entity<Snapshot>().HasKey(x => x.Id);\r\n\r\n            modelBuilder.Entity<Organisation>().ToTable(\"Organisations\");\r\n            modelBuilder.Entity<Organisation>().HasKey(x => x.Id);\r\n            modelBuilder.Entity<Organisation>().HasAlternateKey(x => x.Checksum);\r\n            modelBuilder.Entity<Organisation>().Property(x => x.Checksum).IsConcurrencyToken().HasMaxLength(32);\r\n\r\n            modelBuilder.Entity<User>().ToTable(\"Users\");\r\n            modelBuilder.Entity<User>().HasKey(x => x.Id);\r\n            modelBuilder.Entity<User>().HasAlternateKey(x => x.Checksum);\r\n            modelBuilder.Entity<User>().Property(x => x.Checksum).IsConcurrencyToken().HasMaxLength(32);\r\n            modelBuilder.Entity<User>().Property(x => x.CreatedOn).IsRequired();\r\n\r\n            modelBuilder.Entity<Subscription>().ToTable(\"Subscriptions\");\r\n            modelBuilder.Entity<Subscription>().HasKey(x => x.Id);\r\n            modelBuilder.Entity<Subscription>().HasAlternateKey(x => x.Checksum);\r\n            modelBuilder.Entity<Subscription>().Property(x => x.Checksum).IsConcurrencyToken().HasMaxLength(32);\r\n\r\n            modelBuilder.Entity<Application>().ToTable(\"Applications\");\r\n            modelBuilder.Entity<Application>().HasKey(x => x.Id);\r\n            modelBuilder.Entity<Application>().HasAlternateKey(x => x.Checksum);\r\n            modelBuilder.Entity<Application>().Property(x => x.Checksum).IsConcurrencyToken().HasMaxLength(32);\r\n            modelBuilder.Entity<Application>().HasAlternateKey(x => new { x.Name, x.AppId });\r\n\r\n            modelBuilder.Entity<OrganisationSnapshot>().ToTable(\"OrganisationSnapshots\");\r\n            modelBuilder.Entity<OrganisationSnapshot>().HasKey(x => x.Id);\r\n            modelBuilder.Entity<OrganisationSnapshot>().HasOne(x => x.Snapshot).WithOne(x => x.OrganisationSnapshot);\r\n            modelBuilder.Entity<OrganisationSnapshot>().HasOne(x => x.Organisation).WithMany(x => x.OrganisationSnapshots).HasForeignKey(x => x.OrganisationId).HasPrincipalKey(x => x.Id);\r\n\r\n            modelBuilder.Entity<OrganisationSnapshotSubscription>().ToTable(\"OrganisationSnapshotSubscriptions\");\r\n            modelBuilder.Entity<OrganisationSnapshotSubscription>().HasKey(x => x.Id);\r\n            modelBuilder.Entity<OrganisationSnapshotSubscription>().HasOne(x => x.OrganisationSnapshot).WithMany(x => x.OrganisationSnapshotSubscriptions).HasForeignKey(x => x.OrganisationSnapshotId).HasPrincipalKey(x => x.Id);\r\n            modelBuilder.Entity<OrganisationSnapshotSubscription>().HasOne(x => x.Subscription).WithMany(x => x.OrganisationSnapshotSubscriptions).HasForeignKey(x => x.SubscriptionId).HasPrincipalKey(x => x.Id);\r\n\r\n            modelBuilder.Entity<UserSnapshot>().ToTable(\"UserSnapshots\");\r\n            modelBuilder.Entity<UserSnapshot>().HasKey(x => x.Id);\r\n            modelBuilder.Entity<UserSnapshot>().HasOne(x => x.User).WithMany(x => x.UserSnapshots).HasForeignKey(x => x.UserId).HasPrincipalKey(x => x.Id);\r\n            modelBuilder.Entity<UserSnapshot>().HasOne(x => x.Snapshot).WithMany(x => x.UserSnapshots).HasForeignKey(x => x.SnapshotId).HasPrincipalKey(x => x.Id);\r\n\r\n            modelBuilder.Entity<UserSnapshotSubscription>().ToTable(\"UserSnapshotSubscriptions\");\r\n            modelBuilder.Entity<UserSnapshotSubscription>().HasKey(x => x.Id);\r\n            modelBuilder.Entity<UserSnapshotSubscription>().HasOne(x => x.UserSnapshot).WithMany(x => x.UserSnapshotSubscriptions).HasForeignKey(x => x.UserSnapshotId).HasPrincipalKey(x => x.Id);\r\n            modelBuilder.Entity<UserSnapshotSubscription>().HasOne(x => x.Subscription).WithMany(x => x.UserSnapshotSubscriptions).HasForeignKey(x => x.SubscriptionId).HasPrincipalKey(x => x.Id);\r\n        }\r\n    }\r\n\r\n\r\n    class Program\r\n    {\r\n        static void Main(string[] args)\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\r\n            using (var ctx = new MyContext())\r\n            {\r\n                var correlationId = Guid.NewGuid();\r\n\r\n                var query = ctx.Snapshots.Where(x => x.CorrelationId == correlationId)\r\n                        .Include(x => x.OrganisationSnapshot)\r\n                            .ThenInclude(x => x.Organisation)\r\n                        .Include(x => x.OrganisationSnapshot)\r\n                            .ThenInclude(x => x.OrganisationSnapshotSubscriptions)\r\n                                .ThenInclude(x => x.Subscription)\r\n                                    .ThenInclude(x => x.Application)\r\n                        .Include(x => x.UserSnapshots)\r\n                            .ThenInclude(x => x.User)\r\n                        .Include(x => x.UserSnapshots)\r\n                            .ThenInclude(x => x.UserSnapshotSubscriptions)\r\n                                .ThenInclude(x => x.Subscription)\r\n                                    .ThenInclude(x => x.Application)\r\n                        .Take(10);\r\n\r\n                var result = query.ToList();\r\n\r\n                //var finalCandidateSnapshots = ctx.Snapshots.Select(x => new { x.CorrelationId, x.GenerationTime })\r\n                //        .OrderByDescending(x => x.GenerationTime).Take(300)\r\n                //        .GroupBy(x => x.CorrelationId).Where(x => x.Count() > 75)\r\n                //        .SelectMany(x => x).Distinct();\r\n\r\n                //var result2 = finalCandidateSnapshots.Single(x => x.GenerationTime == finalCandidateSnapshots.Max(cs => cs.GenerationTime)).CorrelationId;\r\n            }\r\n        }\r\n    }\r\n}\r\n\r\n```"}}