0

Using Entity Framework Code First's Seed method, I have been making calls like this to add new objects to a table.

private void SeedDatabaseActivities(SiteDbContext context) { context.Activities.AddOrUpdate(new ActivityDefinition { Id = 605, Name = "bicycling, >20 mph, racing, not drafting", UseSteps = false, UseDistance = false, Category = "bicycling", Mets = 16 }); }

This was working great for months. Recently, this no longer sets the ID, correctly. I have been searching for why this would change, but haven't been able to find the issue.

What would cause this behavior?

Oliver Kane
  • 888
  • 1
  • 6
  • 23
  • Try adding the key: AddOrUpdate(ad => ad.Id, new ActivityDefinition {...}); http://thedatafarm.com/data-access/take-care-with-ef-4-3-addorupdate-method/ – Steve Greene May 01 '15 at 15:50
  • @SteveGreene, I've seen that page, and it's on the right track, but the difference is in that example, they're setting the Name property, and not the Id property. – Oliver Kane May 01 '15 at 17:12
  • Is your Id field defined as identity? – Steve Greene May 01 '15 at 17:42
  • Yes, it's identified as Identity by using the DataAnnotation [Key]. Sometime between now and 6 months ago, this was valid to insert an ID into that column using the AddOrUpdate method but not the Add method. Neither works now, even when I explicedly run SQL that would allow it such as SET IDENTITY_INSERT [TABLE] ON – Oliver Kane May 01 '15 at 19:45
  • Have you tried [DatabaseGenerated(DatabaseGeneratedOption.None)] ? http://stackoverflow.com/questions/15509564/entity-framework-code-first-migrations-set-primary-key-value – Steve Greene May 01 '15 at 20:43
  • @SteveGreene, I have. While that allows me to do the insert, these IDs are intended to be auto-increment. The only exception is the set that I'm adding to the DB at init time. – Oliver Kane May 03 '15 at 02:14

0 Answers0