{"action":"created","issue":{"url":"https://api.github.com/repos/aspnet/EntityFrameworkCore/issues/10180","repository_url":"https://api.github.com/repos/aspnet/EntityFrameworkCore","labels_url":"https://api.github.com/repos/aspnet/EntityFrameworkCore/issues/10180/labels{/name}","comments_url":"https://api.github.com/repos/aspnet/EntityFrameworkCore/issues/10180/comments","events_url":"https://api.github.com/repos/aspnet/EntityFrameworkCore/issues/10180/events","html_url":"https://github.com/aspnet/EntityFrameworkCore/issues/10180","id":269437417,"number":10180,"title":"Unable to remove item by its PK only","user":{"login":"silarmani","id":33187297,"avatar_url":"https://avatars3.githubusercontent.com/u/33187297?v=4","gravatar_id":"","url":"https://api.github.com/users/silarmani","html_url":"https://github.com/silarmani","followers_url":"https://api.github.com/users/silarmani/followers","following_url":"https://api.github.com/users/silarmani/following{/other_user}","gists_url":"https://api.github.com/users/silarmani/gists{/gist_id}","starred_url":"https://api.github.com/users/silarmani/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/silarmani/subscriptions","organizations_url":"https://api.github.com/users/silarmani/orgs","repos_url":"https://api.github.com/users/silarmani/repos","events_url":"https://api.github.com/users/silarmani/events{/privacy}","received_events_url":"https://api.github.com/users/silarmani/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2017-10-29T21:27:39Z","updated_at":"2017-10-29T23:19:25Z","closed_at":null,"author_association":"NONE","body":"If an entity has extra indices then I am unable to delete it by its PK only\r\n\r\nExample\r\n\r\n```\r\n    class Program\r\n    {\r\n        static int Main(string[] args)\r\n        {\r\n            var ctx = new MyDbContext();\r\n\r\n            **var toBeDeleted = ctx.RiskConsequenceSeverityGroupItems\r\n                .Select(e => new RiskConsequenceSeverityGroupItem { Id = e.Id });**\r\n\r\n            ctx.RiskConsequenceSeverityGroupItems.RemoveRange(toBeDeleted);\r\n\r\n            ctx.SaveChanges();\r\n\r\n            return 0;\r\n        }\r\n    }\r\n\r\n    public class RiskConsequenceSeverityGroupItem\r\n    {\r\n        public int Id { get; set; }\r\n\r\n        public int Order { get; set; }\r\n\r\n        public string Name { get; set; }\r\n    }\r\n\r\n    public class MyDbContext : DbContext\r\n    {\r\n        public DbSet<RiskConsequenceSeverityGroupItem> RiskConsequenceSeverityGroupItems { get; set; }\r\n\r\n        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)\r\n        {\r\n            optionsBuilder.UseSqlServer(\"Server=localhost,1433;Database=ipl_tux;Integrated Security=True;\");            \r\n        }\r\n\r\n        protected override void OnModelCreating(ModelBuilder modelBuilder)\r\n        {\r\n            modelBuilder.Entity<RiskConsequenceSeverityGroupItem>()\r\n                .HasIndex(e => new { e.Order })\r\n                .IsUnique();\r\n\r\n            base.OnModelCreating(modelBuilder);\r\n        }\r\n    }\r\n```\r\n\r\nIn this case I get the following execption\r\n\r\nUnhandled Exception: System.ArgumentException: An item with the same key has already been added. Key: System.Object[]\r\n   at System.ThrowHelper.ThrowAddingDuplicateWithKeyArgumentException(Object key)\r\n   at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)\r\n   at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)\r\n   at Microsoft.EntityFrameworkCore.Update.Internal.CommandBatchPreparer.AddUniqueValueEdges(Multigraph`2 commandGraph)\r\n   at Microsoft.EntityFrameworkCore.Update.Internal.CommandBatchPreparer.TopologicalSort(IEnumerable`1 commands)\r\n   at Microsoft.EntityFrameworkCore.Update.Internal.CommandBatchPreparer.<BatchCommands>d__8.MoveNext()\r\n   at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(Tuple`2 parameters)\r\n   at Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)\r\n   at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.Execute[TState,TResult](IExecutionStrategy strategy, TState state, Func`2 operation)\r\n   at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(IEnumerable`1 commandBatches, IRelationalConnection connection)\r\n   at Microsoft.EntityFrameworkCore.Storage.RelationalDatabase.SaveChanges(IReadOnlyList`1 entries)\r\n   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(IReadOnlyList`1 entriesToSave)\r\n   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess)\r\n   at Microsoft.EntityFrameworkCore.DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess)\r\n   at Microsoft.EntityFrameworkCore.DbContext.SaveChanges()\r\n   at EfTests.Program.Main(String[] args) in D:\\code\\tuxedo\\Tests\\EfBugTest.cs:line 17\r\n\r\nHowever, if I specify the extra column which has an index on then it works fine\r\n\r\nExample\r\n\r\n```\r\n            var toBeDeleted = ctx.RiskConsequenceSeverityGroupItems\r\n                .Select(e => new RiskConsequenceSeverityGroupItem { Id = e.Id**, Order = e.Order** });\r\n\r\n            ctx.RiskConsequenceSeverityGroupItems.RemoveRange(toBeDeleted);\r\n\r\n            ctx.SaveChanges();\r\n```\r\n\r\nHaving to specify that extra column seems to be irrelevant to me if all want to do is to delete it. It works even if I assign random numbers to it.\r\n\r\nExample\r\n\r\n```\r\n            var toBeDeleted = ctx.RiskConsequenceSeverityGroupItems\r\n                .Select(e => new RiskConsequenceSeverityGroupItem { Id = e.Id**, Order = new Random().Next()** });\r\n\r\n            ctx.RiskConsequenceSeverityGroupItems.RemoveRange(toBeDeleted);\r\n\r\n            ctx.SaveChanges();\r\n```\r\n"},"comment":{"url":"https://api.github.com/repos/aspnet/EntityFrameworkCore/issues/comments/340311456","html_url":"https://github.com/aspnet/EntityFrameworkCore/issues/10180#issuecomment-340311456","issue_url":"https://api.github.com/repos/aspnet/EntityFrameworkCore/issues/10180","id":340311456,"user":{"login":"smitpatel","id":1528107,"avatar_url":"https://avatars0.githubusercontent.com/u/1528107?v=4","gravatar_id":"","url":"https://api.github.com/users/smitpatel","html_url":"https://github.com/smitpatel","followers_url":"https://api.github.com/users/smitpatel/followers","following_url":"https://api.github.com/users/smitpatel/following{/other_user}","gists_url":"https://api.github.com/users/smitpatel/gists{/gist_id}","starred_url":"https://api.github.com/users/smitpatel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/smitpatel/subscriptions","organizations_url":"https://api.github.com/users/smitpatel/orgs","repos_url":"https://api.github.com/users/smitpatel/repos","events_url":"https://api.github.com/users/smitpatel/events{/privacy}","received_events_url":"https://api.github.com/users/smitpatel/received_events","type":"User","site_admin":false},"created_at":"2017-10-29T23:19:25Z","updated_at":"2017-10-29T23:19:25Z","author_association":"CONTRIBUTOR","body":"Not a duplicate."}}
{"actor":{"display_login":"smitpatel"}}