{"action":"opened","issue":{"url":"https://api.github.com/repos/aspnet/EntityFramework/issues/2210","labels_url":"https://api.github.com/repos/aspnet/EntityFramework/issues/2210/labels{/name}","comments_url":"https://api.github.com/repos/aspnet/EntityFramework/issues/2210/comments","events_url":"https://api.github.com/repos/aspnet/EntityFramework/issues/2210/events","html_url":"https://github.com/aspnet/EntityFramework/issues/2210","id":77093896,"number":2210,"title":"Beta5 2032: DbSet.Add, DbContext.Add & Entry.State=Added setting key value of entity","user":{"login":"julielerman","id":5007120,"avatar_url":"https://avatars.githubusercontent.com/u/5007120?v=3","gravatar_id":"","url":"https://api.github.com/users/julielerman","html_url":"https://github.com/julielerman","followers_url":"https://api.github.com/users/julielerman/followers","following_url":"https://api.github.com/users/julielerman/following{/other_user}","gists_url":"https://api.github.com/users/julielerman/gists{/gist_id}","starred_url":"https://api.github.com/users/julielerman/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/julielerman/subscriptions","organizations_url":"https://api.github.com/users/julielerman/orgs","repos_url":"https://api.github.com/users/julielerman/repos","events_url":"https://api.github.com/users/julielerman/events{/privacy}","received_events_url":"https://api.github.com/users/julielerman/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-05-16T18:08:04Z","updated_at":"2015-05-16T18:08:04Z","closed_at":null,"body":"This is new behavior and I can't imagine it's intended. It did not work like this in Beta3. And in Beta4, the Add method started doing this but Entry.State=Added didn't.\r\n\r\nWhen you call any of these methods on a new object (who's key property is an integer), the Id is gettinig populated.\r\n\r\nI tried this with InMemory and SQLServer providers.\r\n\r\nSimple as can be:\r\n\r\nMy class:\r\n public class LambsonType\r\n  {\r\n    public int Id { get; set; }\r\n    public string Description { get; set; }\r\n  }\r\n\r\nMy context: (note that you can't pass in dboptions with preset provider so I have a workaround)\r\n\r\n public class LambsonContext : DbContext\r\n  {\r\n    bool _isInMemory = false;\r\n    public DbSet<LambsonType> Lambsies { get; set; }\r\n    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {\r\n      if (_isInMemory)\r\n        optionsBuilder.UseInMemoryStore();\r\n      else\r\n        optionsBuilder.UseSqlServer\r\n          (\"Server = (localdb)\\\\mssqllocaldb; Database=EF7Bryce; Trusted_Connection=True; MultipleActiveResultSets = True;\");\r\n      base.OnConfiguring(optionsBuilder);\r\n\r\n    }\r\n    public LambsonContext(bool isInMemory) : base() {\r\n      _isInMemory = isInMemory;\r\n    }\r\n  }\r\n\r\nA set of tests. The first three use the inmemory provider and are just adding the new object to the context. The second three use SqlServer provider and just add (no saving).\r\n\r\nWIth the inmemory, if you run them all together, the Id gets changed to 1, 2 and 3 in each subsequent test.\r\n\r\nWith Sqlserver it's wierd because if I run the three tests, I first got 1, 11 and 21. ran them again and got 31, 41 and 51.\r\n\r\nWith these getting applied you will have a conflict if there are already objects being tracked by teh context with those key values ...this happened to me which is how I backed up to this problem.\r\n\r\nhope it's useful.\r\n\r\nAlso there is another problem I noticed re using DbContextOptionsBuilder. DbContext still takes DbContextOptions as an overload. I will look for a related issue and add if necesary then come back here and add a pointer.\r\n  [TestMethod]\r\n    public void InMemoryAddNewEntityViaDbSetAddMethod() {\r\n      var lamb = new LambsonType { Description = \"a type\" };\r\n      var lambidbeforeadd = lamb.Id;\r\n     // Console.WriteLine(\"before adding to context, new lamb ID is {0}\", lamb.ID);\r\n      using (var ctx = new LambsonContext(true)) {\r\n        ctx.Lambsies.Add(lamb);\r\n        Assert.AreEqual(lambidbeforeadd,lamb.Id);\r\n         Console.WriteLine(\"InMemory: After DbSet.Add, new lamb ID is {0}\", lamb.Id);\r\n      }\r\n\r\n    }\r\n    [TestMethod]\r\n    public void InMemoryAddNewEntityViaDbContextAddMethod() {\r\n      var lamb = new LambsonType { Description = \"a type\" };\r\n      var lambidbeforeadd = lamb.Id;\r\n      // Console.WriteLine(\"before adding to context, new lamb ID is {0}\", lamb.ID);\r\n      using (var ctx = new LambsonContext(true)) {\r\n        ctx.Add(lamb);\r\n        Assert.AreEqual(lambidbeforeadd, lamb.Id);\r\n        Console.WriteLine(\"InMemory: After DbContext.Add, new lamb ID is {0}\", lamb.Id);\r\n      }\r\n\r\n    }\r\n    [TestMethod]\r\n    public void InMemoryAddNewEntityViaEntryState() {\r\n      var lamb = new LambsonType { Description = \"a type\" };\r\n      var lambidbeforeadd = lamb.Id;\r\n       using (var ctx = new LambsonContext(true)) {\r\n        ctx.Entry(lamb).State = Microsoft.Data.Entity.EntityState.Added;\r\n        Assert.AreEqual(lambidbeforeadd, lamb.Id);\r\n        Console.WriteLine(\"InMemory: After DbContext.Entry.State, new lamb ID is {0}\", lamb.Id);\r\n\r\n      }\r\n\r\n    }\r\n\r\n    [TestMethod]\r\n    public void SqlServerAddNewEntityViaDbSetAddMethod() {\r\n      var lamb = new LambsonType { Description = \"a type\" };\r\n      var lambidbeforeadd = lamb.Id;\r\n      using (var ctx = new LambsonContext(false)) {\r\n        ctx.Database.EnsureCreated();\r\n        ctx.Lambsies.Add(lamb);\r\n        Assert.AreEqual(lambidbeforeadd, lamb.Id);\r\n        Console.WriteLine(\"SqlServer: After DbSet.Add, new lamb ID is {0}\", lamb.Id);\r\n\r\n      }\r\n\r\n    }\r\n    [TestMethod]\r\n    public void SqlServerAddNewEntityViaDbContextAddMethod() {\r\n      var lamb = new LambsonType { Description = \"a type\" };\r\n      var lambidbeforeadd = lamb.Id;\r\n      using (var ctx = new LambsonContext(false)) {\r\n        ctx.Database.EnsureCreated();\r\n\r\n        ctx.Add(lamb);\r\n        Assert.AreEqual(lambidbeforeadd, lamb.Id);\r\n        Console.WriteLine(\"SqlServer: After DbContext.Add, new lamb ID is {0}\", lamb.Id);\r\n      }\r\n\r\n    }\r\n    [TestMethod]\r\n    public void SqlServerAddNewEntityViaEntryState() {\r\n      var lamb = new LambsonType { Description = \"a type\" };\r\n      var lambidbeforeadd = lamb.Id;\r\n      using (var ctx = new LambsonContext(false)) {\r\n        ctx.Database.EnsureCreated();\r\n\r\n        ctx.Entry(lamb).State = EntityState.Added;\r\n        Assert.AreEqual(lambidbeforeadd, lamb.Id);\r\n        Console.WriteLine(\"SqlServer: After DbContext.Entry.State, new lamb ID is {0}\", lamb.Id);\r\n      }\r\n\r\n    }"}}