Entity Framework – Use a Guid as the primary key

Using a Guid as your tables primary key, when using Entity Framework, requires a little more effort than when using a integer.  The setup process is straightforward, after you’ve read/been shown how to do it.

The process is slightly different for the Code First and Database First approaches.  This post discusses both techniques.

Code First

Using a Guid as the primary key when taking the code first approach is simple.  When creating your entity, add the DatabaseGenerated attribute to your primary key property, as shown below;

public Guid Id { get; set; }

Entity framework will create the column as you would expect, with a primary key and uniqueidentifier data type.

Click to zoom in

Also notice, very important, that the default value on the column has been set to (newsequentialid()).  This generates a new sequential (continuous) Guid for each row.  If you were so inclined, you could change this to newid()), which would result in a completely random Guid for each new row.  This will be cleared each time your database gets dropped and re-created, so this works better when taking the Database First approach.

Database First

The database first approach follows a similar line to the code first approach, but you’ll have to manually edit your model to make it work.

Ensure that you edit the primary key column and add the (newsequentialid()) or (newid()) function as the default value before doing anything.

Next, open you EDMX diagram, select the appropriate property and open the properties window.  Ensure that StoreGeneratedPattern is set to identity.

Click the image to zoom in

No need to give your entity an ID in your code, that will be populated for you automatically after the entity has been commited to the database;

using (ApplicationDbContext context = new ApplicationDbContext())
    var person = new Person
                         FirstName = "Random",
                         LastName = "Person";


Important Note: Your Guid field MUST be a primary key, or this does not work.  Entity Framework will give you a rather cryptic error message!


Guid (Globally Unique Identifiers) can easily be used as primary keys in Entity Framework.  A little extra effort is required to do this, depending on which approach you are taking.  When using the code first approach, add the DatabaseGenerated attribute to your key field.  When taking the Database First approach, explicitly set the StoredGeneratedPattern to Identity on your model.


  • Morten Albertsen

    February 7, 2017

    What are any argument for having the Id property settable? My counter-argument would be that once a unique id is set it should never be changed again.

    • Thiago Daher

      April 13, 2017

      I agree, Morten…
      To avoid change Id property, you can declare it as internal? Yes
      EntityFramework can access and changes this property? Yes, since you have insert an assembly attribute named “InternalsVisibleTo”.
      In this case, the KeyAttribute does not work to set the property as key… But it can be done in FluentApi and works as well…
      I am not sure about the attribute that determines the “auto generated”… Maybe, as the KeyAttribute, it cannot work… Again, FluentApi and its done!


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.