DotnetSparrow
DotnetSparrow

Reputation: 27996

Cannot update identity column 'Unique ID', Entity Framework Error

I am trying to update my entity using Entity Framework 4.1 code First approach like this(In my Repository Class):

internal void SaveAccount(Account account) {

            context.Entry(account).State = EntityState.Modified;
            context.SaveChanges();
        }

but I am getting an error which is:

Cannot update identity column 'Unique ID'.

I am not updating Unique ID Column. How can I avoid this error ?

Upvotes: 4

Views: 3781

Answers (2)

Claire G
Claire G

Reputation: 91

If the property is the primary key, the following attribute should be added instead:

[Key]

Upvotes: 1

DotnetSparrow
DotnetSparrow

Reputation: 27996

I added this attribute to my Model property and error gone:

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]

Upvotes: 6

Related Questions