One of the tests I wanted to do was to find out how EF5 handles auto-incremental GUIDs. So here goes...
Reproducing the problem
I will be using database-first model using SQL Server 2012 Express.
I have highlighted the important areas. Basically table has a "Status" column that is set to use "NewId()" binding. This will automatically create a Guid when a new row is inserted.
Thereafter a new conceptual model was created. (See below.)
Once the corresponding EDMX file is created, it will be opened in the Visual Studio editor by default. As there is only a single table in the database, one entity will be created.
Keep a close eye on the "StoreGeneratedPattern". This properties is what EF uses to determine the value population strategy of the given column. The available options are "Identity" and "Computed".
We set the binding in the SQL Server to auto populate the "Status" column (NewId()). However when we add a new row to the table using EF, following is what we get.
This is most surely not that we want. The "Status" value is set to the default Guid value.
Fixing the issue
Actually the fixing the problem is pretty easy... What we need to do is to return to the conceptual model and update the "StoreGeneratedPattern" of "Status" property to "Computed" as below.By updating the property to "Computed"; it notifies the EF that the value of the property is calculated by the store (in this case SQL Server).
Just watch out!
Thank you
ReplyDelete