I am using EF4 in my C# project. The problem I’m faced with is that, when I try to save a record, I get a Primary Key Violation and the PK value is “0000-0000-0000-xxx”. From my guess, EF does not recognize the IsIdentity flag and generate a guid value. In my SQL Server database for my table, I specify a PK(uniqueidentifier>guid) and also set it as the IdentityColumn, thus I would expect this to travel through to my project as such since the guid is generated in SQL Server. Is there a way to overcome this bug?
Share
You must specify your
GuidID as an Identity in your EF model. In EF 4.1:(
GuidIds are not identities by default, in contrast tointIds.)If you create the database with this model EF will create a column with a default value of
newid().In EF 4.0 you can go to the model designer, mark your Id property in the designer, open the properties window and then set
StoreGeneratedPatterntoIdentity.