3

Have a problem with incrementing. I created a new object and tried set it into my DB I received an error of data violation. The index in table wasn't increased (Id=0). Id - set as primary key in SQL table and the StoredGeneratedPattern property of field "Id" in EDM set as "Identity" so, obviously, it must be incremented automatically.

public void AddPhone(UserPhone phone)
{
    context.AddToUserPhone(phone);
    context.SaveChanges();
}

I can't understand why.

Eranga
  • 32,181
  • 5
  • 97
  • 96
Vengrovskyi
  • 307
  • 1
  • 4
  • 9

1 Answers1

5

Entity Framework does not automatically increment IDs. That's the database's job. Set the ID column on the database table as an IDENTITY column so that it will auto-increment. Then you should find that after you SaveChanges() the phone's ID property will have been set to the value the database chose for it.

StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315