Reputation: 2424
I am using Entity framework 4.1
I have regular object, and I set his ID to be something like 123456789 (long value)
Then I call SaveChanges()
the problem is SaveChanges() is changing my ID both in the memory and inside the database it assign the ID to be 20
and if I will repeat this it will assign the id to be 21 and so on..
What can cause this behavior? and what should I do to solve it?
ID declaration: public long ID { get; set; }
It have been worked before, it may worked before when the ID was declared as int (change it back to check is too large modification..)
Thanks
Upvotes: 1
Views: 199
Reputation: 160862
the problem is SaveChanges() is changing my ID both in the memory and inside the database it assign the ID to be 20
Most likely the ID column in the database is an Identity column, so the Id gets assigned by the database, not by you. If you really want to assign the Id value yourself, remove the Identity property of the column.
Assuming you are not using code first and you are working with Sql Server open up the table in SQL Server Management Studio and go to the column properties for the ID column - select "(Is Identity) - No".
Edit:
For code first you can force a non-identity key using the attributes
[Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
on the ID property. Is there a particular use case why you want to do this? Usually its easier letting the database handle uniqueness of the key.
Upvotes: 5
Reputation: 9314
The following annotation for the key in your class should fix it for you
[DatabaseGenerated(DatabaseGenerationOption.None)]
Upvotes: 2