{"action":"created","issue":{"url":"https://api.github.com/repos/aspnet/EntityFrameworkCore/issues/12984","repository_url":"https://api.github.com/repos/aspnet/EntityFrameworkCore","labels_url":"https://api.github.com/repos/aspnet/EntityFrameworkCore/issues/12984/labels{/name}","comments_url":"https://api.github.com/repos/aspnet/EntityFrameworkCore/issues/12984/comments","events_url":"https://api.github.com/repos/aspnet/EntityFrameworkCore/issues/12984/events","html_url":"https://github.com/aspnet/EntityFrameworkCore/issues/12984","id":350042933,"node_id":"MDU6SXNzdWUzNTAwNDI5MzM=","number":12984,"title":"Collection navigations should use reference equality by default","user":{"login":"Anirie","id":37371006,"node_id":"MDQ6VXNlcjM3MzcxMDA2","avatar_url":"https://avatars3.githubusercontent.com/u/37371006?v=4","gravatar_id":"","url":"https://api.github.com/users/Anirie","html_url":"https://github.com/Anirie","followers_url":"https://api.github.com/users/Anirie/followers","following_url":"https://api.github.com/users/Anirie/following{/other_user}","gists_url":"https://api.github.com/users/Anirie/gists{/gist_id}","starred_url":"https://api.github.com/users/Anirie/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Anirie/subscriptions","organizations_url":"https://api.github.com/users/Anirie/orgs","repos_url":"https://api.github.com/users/Anirie/repos","events_url":"https://api.github.com/users/Anirie/events{/privacy}","received_events_url":"https://api.github.com/users/Anirie/received_events","type":"User","site_admin":false},"labels":[{"id":936619505,"node_id":"MDU6TGFiZWw5MzY2MTk1MDU=","url":"https://api.github.com/repos/aspnet/EntityFrameworkCore/labels/customer-reported","name":"customer-reported","color":"a4a5f9","default":false},{"id":76481852,"node_id":"MDU6TGFiZWw3NjQ4MTg1Mg==","url":"https://api.github.com/repos/aspnet/EntityFrameworkCore/labels/type-bug","name":"type-bug","color":"f7c6c7","default":false}],"state":"closed","locked":false,"assignee":{"login":"maumar","id":1591930,"node_id":"MDQ6VXNlcjE1OTE5MzA=","avatar_url":"https://avatars3.githubusercontent.com/u/1591930?v=4","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},"assignees":[{"login":"maumar","id":1591930,"node_id":"MDQ6VXNlcjE1OTE5MzA=","avatar_url":"https://avatars3.githubusercontent.com/u/1591930?v=4","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/EntityFrameworkCore/milestones/47","html_url":"https://github.com/aspnet/EntityFrameworkCore/milestone/47","labels_url":"https://api.github.com/repos/aspnet/EntityFrameworkCore/milestones/47/labels","id":3740595,"node_id":"MDk6TWlsZXN0b25lMzc0MDU5NQ==","number":47,"title":"2.2.0","description":"","creator":{"login":"ajcvickers","id":1430078,"node_id":"MDQ6VXNlcjE0MzAwNzg=","avatar_url":"https://avatars1.githubusercontent.com/u/1430078?v=4","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},"open_issues":23,"closed_issues":6,"state":"open","created_at":"2018-10-15T17:48:39Z","updated_at":"2018-10-17T22:37:53Z","due_on":null,"closed_at":null},"comments":2,"created_at":"2018-08-13T14:01:23Z","updated_at":"2018-10-17T22:37:53Z","closed_at":"2018-10-17T22:37:53Z","author_association":"NONE","body":"I encounter a strange behaviour while working with EF Core 2.1.1 . \r\n\r\nYou can find a small program for reproduction here: [https://github.com/Anirie/EFCoreAddMethodBug](url)\r\n\r\nThis code copies an existing \"Book\" entity with all it references. \r\n\r\nIn detail, it takes an existing “book”, resets the db connection (so the chosen book entity is untracked), deletes all key properties of the entity (including key properties of all children) and adds the entity to the context.\r\n\r\n\r\n\r\n```C#\r\n            var options = SqliteInMemory.CreateOptions<TestModelContext>();\r\n            var dbContext = new TestModelContext(options);\r\n            dbContext.Database.EnsureCreated();\r\n\r\n            //create book with one page and one image ( image has navigation props to book and bookpage)\r\n            var book = CreateBasicBook();\r\n            var page = CreateBasicBookPage(book);\r\n            var image = CreateBasicImage(book, page);\r\n\r\n            dbContext.Add(book);\r\n            dbContext.Add(page);\r\n            dbContext.Add(image);\r\n            dbContext.SaveChanges();\r\n\r\n            //take existing book\r\n            var bookToCopy =dbContext.Books.Include(b => b.BookPages).Include(b => b.Images).First() as Book;\r\n\r\n            //reset Connection to Database\r\n            dbContext.Dispose();\r\n            dbContext = new TestModelContext(options);\r\n\r\n            // set keyproperties to zero\r\n            bookToCopy.Id = 0;\r\n            //set keyproperties of children to zero\r\n            foreach(var i in bookToCopy.Images)\r\n            {\r\n                i.Id = 0;\r\n            }\r\n            foreach(var p in bookToCopy.BookPages)\r\n            {\r\n                p.Id = 0;\r\n            }\r\n\r\n            //add to context\r\n            dbContext.Add(bookToCopy);\r\n            //save context\r\n            dbContext.SaveChanges();\r\n\r\n            //reset Connection to Database changes everything\r\n            //dbContext.Dispose();\r\n            //dbContext = new TestModelContext(options);\r\n\r\n            //output\r\n            var allBooks = dbContext.Books.Include(b => b.BookPages).Include(b => b.Images).ToList();\r\n  \r\n            Console.WriteLine(\"Initial Book: \"+ JsonConvert.SerializeObject(allBooks[0], new JsonSerializerSettings\r\n            {\r\n               ReferenceLoopHandling = ReferenceLoopHandling.Ignore\r\n            }));\r\n            Console.WriteLine(\"Copied Book: \" + JsonConvert.SerializeObject(allBooks[1], new JsonSerializerSettings\r\n            {\r\n                ReferenceLoopHandling = ReferenceLoopHandling.Ignore\r\n            }));\r\n```\r\n\r\nNow the context contains two books (the initial book and its copy), but the copy holds **two** identical bookPages instead of one. Somehow, invoking dbContext.Add(bookToCopy) adds a second bookpage to the entity. **It seems that adding an entity with many references to the dbContext creates unwanted navigation properties.**I think that is not the intended behaviour of the dbContext.Add method.\r\nStrangely enough, if I reset the dbContext after I added the book entity everything works fine. That means, I get a context the contains the initial book, a copy of this book and all references are set in the right way.  (For reproduction uncomment line 60 and 61 in the provided code)\r\n\r\nIs it possible that the Add method contains a bug? Or is it intended to reset the DBConnection everytime after SaveChanges() method was invoked?\r\n\r\n### Steps to reproduce\r\nYou can finde the whole project here: [https://github.com/Anirie/EFCoreAddMethodBug](url)\r\n\r\n### Further technical details\r\nEF Core version: 2.1.1\r\nDatabase Provider:  Microsoft.EntityFrameworkCore.Sqlite\r\nOperating system: Microsoft Windows 10 Pro\r\nIDE: Visual Studio 2017 15.4\r\n"},"comment":{"url":"https://api.github.com/repos/aspnet/EntityFrameworkCore/issues/comments/430813645","html_url":"https://github.com/aspnet/EntityFrameworkCore/issues/12984#issuecomment-430813645","issue_url":"https://api.github.com/repos/aspnet/EntityFrameworkCore/issues/12984","id":430813645,"node_id":"MDEyOklzc3VlQ29tbWVudDQzMDgxMzY0NQ==","user":{"login":"maumar","id":1591930,"node_id":"MDQ6VXNlcjE1OTE5MzA=","avatar_url":"https://avatars3.githubusercontent.com/u/1591930?v=4","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},"created_at":"2018-10-17T22:37:53Z","updated_at":"2018-10-17T22:37:53Z","author_association":"CONTRIBUTOR","body":"fixed in aaa92d9d6f6fc20bb274f7b1afb20554bb20c96d"}}
{"actor":{"display_login":"maumar"}}