Vengrovskyi
Vengrovskyi

Reputation: 307

Entity Framework Id auto incrementing

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.

Upvotes: 3

Views: 5323

Answers (1)

StriplingWarrior
StriplingWarrior

Reputation: 156654

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.

Upvotes: 5

Related Questions