{"action":"closed","issue":{"url":"https://api.github.com/repos/aspnet/EntityFramework/issues/5133","repository_url":"https://api.github.com/repos/aspnet/EntityFramework","labels_url":"https://api.github.com/repos/aspnet/EntityFramework/issues/5133/labels{/name}","comments_url":"https://api.github.com/repos/aspnet/EntityFramework/issues/5133/comments","events_url":"https://api.github.com/repos/aspnet/EntityFramework/issues/5133/events","html_url":"https://github.com/aspnet/EntityFramework/issues/5133","id":149852907,"number":5133,"title":"EF 7 Wont Save Changes on Entity With Many-to-Many Relationships","user":{"login":"ElvisHalil","id":6643233,"avatar_url":"https://avatars.githubusercontent.com/u/6643233?v=3","gravatar_id":"","url":"https://api.github.com/users/ElvisHalil","html_url":"https://github.com/ElvisHalil","followers_url":"https://api.github.com/users/ElvisHalil/followers","following_url":"https://api.github.com/users/ElvisHalil/following{/other_user}","gists_url":"https://api.github.com/users/ElvisHalil/gists{/gist_id}","starred_url":"https://api.github.com/users/ElvisHalil/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ElvisHalil/subscriptions","organizations_url":"https://api.github.com/users/ElvisHalil/orgs","repos_url":"https://api.github.com/users/ElvisHalil/repos","events_url":"https://api.github.com/users/ElvisHalil/events{/privacy}","received_events_url":"https://api.github.com/users/ElvisHalil/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/aspnet/EntityFramework/labels/investigation-needed","name":"investigation-needed","color":"0052cc"},{"url":"https://api.github.com/repos/aspnet/EntityFramework/labels/pri0","name":"pri0","color":"e11d21"}],"state":"closed","locked":false,"assignee":{"login":"ajcvickers","id":1430078,"avatar_url":"https://avatars.githubusercontent.com/u/1430078?v=3","gravatar_id":"","url":"https://api.github.com/users/ajcvickers","html_url":"https://github.com/ajcvickers","followers_url":"https://api.github.com/users/ajcvickers/followers","following_url":"https://api.github.com/users/ajcvickers/following{/other_user}","gists_url":"https://api.github.com/users/ajcvickers/gists{/gist_id}","starred_url":"https://api.github.com/users/ajcvickers/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ajcvickers/subscriptions","organizations_url":"https://api.github.com/users/ajcvickers/orgs","repos_url":"https://api.github.com/users/ajcvickers/repos","events_url":"https://api.github.com/users/ajcvickers/events{/privacy}","received_events_url":"https://api.github.com/users/ajcvickers/received_events","type":"User","site_admin":false},"milestone":{"url":"https://api.github.com/repos/aspnet/EntityFramework/milestones/7","html_url":"https://github.com/aspnet/EntityFramework/milestones/1.0.0","labels_url":"https://api.github.com/repos/aspnet/EntityFramework/milestones/7/labels","id":666128,"number":7,"title":"1.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":196,"closed_issues":122,"state":"open","created_at":"2014-05-21T19:25:32Z","updated_at":"2016-04-27T21:08:16Z","due_on":null,"closed_at":null},"comments":6,"created_at":"2016-04-20T18:44:17Z","updated_at":"2016-04-27T21:08:16Z","closed_at":"2016-04-27T21:08:16Z","body":"I'm not sure if I'm doing something completely wrong (if so, PLEASE let me know!), but I am trying to add to a entity (Devices) which has some relationships using Code First (EF Core).  Here is the model in question:\r\n\r\n    [Table(\"Devices\")]\r\n    public class Device\r\n    {\r\n        public int ID { get; set; }\r\n\r\n        public string SerialNumber { get; set; }\r\n\r\n        public int DeviceModelID { get; set; }\r\n\r\n        public virtual DeviceModel DeviceModel { get; set; }\r\n\r\n        public ICollection<UnitDevice> UnitDevice { get; set; }\r\n\r\n        public ICollection<DeviceTest> DeviceTest { get; set; }\r\n    }\r\n\r\nIt has a 1 to Many relationship from Device to DeviceModel.  DeviceModel is seeded data and that part seems to be OK, no real issues there.  My issue is when I try to add a Device and call SaveChanges I get an error regarding UnitDevice and DeviceTest, duplicate records.  Both UnitDevice and DeviceTest are Many-Many relationships that have 2 composite P-keys.  Here are their models for more context:\r\n\r\nDeviceTest:\r\n\r\n    [Table(\"DeviceTests\")]\r\n    public class DeviceTest\r\n    {\r\n        public Guid TestID { get; set; }\r\n\r\n        public int DeviceID { get; set; }\r\n\r\n        public TestResult Test { get; set; }\r\n\r\n        public Device Device { get; set; }\r\n    }\r\n\r\nUnitDevice:\r\n\r\n    [Table(\"UnitDevices\")]\r\n    public class UnitDevice\r\n    {\r\n        public Guid SessionZoneID { get; set; }\r\n\r\n        public int DeviceID { get; set; }\r\n\r\n        public ZonesInSession ZonesInSession { get; set; }\r\n\r\n        public Device Device { get; set; }\r\n    }\r\n\r\nMy issue arises when the following code executes:\r\n\r\n                foreach(var device in devices)\r\n                {\r\n                    if (_deviceRepository.FindBy(x => x.ID == device.ID).FirstOrDefault() == null)\r\n                    {\r\n                        _deviceRepository.Add(device);\r\n                        _deviceRepository.Save();\r\n                    }\r\n                }\r\n\r\nDevices is a in memory List of Device (model can be seen at the top).  In some cases it will throw an error on the save.  One particular example I will provide has the following data in device:\r\n\r\nDeviceModel = null\r\nDeviceModelID = 5\r\nDeviceTest = null\r\nID = 0\r\nSerialNumber = \"GIZ0011\"\r\nUnitDevice = null\r\n\r\nOnce this device goes through the .Add(device) the ID is changed to:\r\n\r\nID = -5\r\n\r\nThe rest of the attributes stay the same, the ID isn't really a concern as I imagine that is just the way EF is 'Adding' and it resolves to the correct ID once it's been saved.  As you can see I am not referencing any UnitDevice objects or DeviceTest objects, they are both null.  Now I am confused as to why when calling SaveChanges on this particular device it throws the following error:\r\n\r\n```\r\nViolation of PRIMARY KEY constraint 'PK_DeviceTest'. Cannot insert duplicate key in object 'dbo.DeviceTests'. The duplicate key value is (69, 480006d2-fc98-4b9f-a561-e84e37ee3df3).\r\nViolation of PRIMARY KEY constraint 'PK_UnitDevice'. Cannot insert duplicate key in object 'dbo.UnitDevices'. The duplicate key value is (f3a0811b-926b-4d8d-8adf-7c3b2c9b3071, 67).\r\nThe statement has been terminated.\r\nThe statement has been terminated.\r\n```\r\n\r\nI am not adding either of those records to either table and have no idea where these values are even coming from.  The Device with SerialNumber \"GIZ0011\" does NOT exist in the Devices table, and all I want to do is Insert it there, but it seems there is something preventing that and I'm not sure if it's my fault or a EF 7 bug. \r\n\r\nPlease help as I'm losing my mind here.  Thanks all!"}}