title: How to Handle Primary Key in Entity Framework 5 Code First
body: <p>In my EF5 code-first models, creation of new records works better if the database sets the primary key. I am using a Guid for primary key and if <code>DatabaseGeneratedOption.Identity</code> is set, SQL Server will always create the <code>uniqueidentifier</code>. </p>
  
  <p>However, this causes issues when I am trying to initially seed the database. If I set the Guid in my seed method, SQL Server overrides it. If I don't set the Guid, I get a new record every time. What is a recommended solution to seed the database using pre-set Guids and keep <code>DatabaseGeneratedOption.Identity</code> set for my normal operations?</p>
  
  <p>Example class model:</p>
  
  <pre><code>[Key]
  [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
  public Guid Id { get; set; }
  public RecordName { get; set; }
  public DateTime? Created { get; set; }
  public DateTime? Updated { get; set; }
  </code></pre>
  
  <p>Example seed method:</p>
  
  <pre><code>var record = new Record()
              {
                  Id = new Guid("3B80725E-9550-4933-807F-C2FAA0942225"),
                  RecordName = "New Record",
                  Created = DateTime.UtcNow,
                  Updated = DateTime.UtcNow,
               };
  context.Record.AddOrUpdate(record);
  context.SaveChanges();
  </code></pre>