Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8402471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:04:35+00:00 2026-06-09T22:04:35+00:00

a.k.a How can we create multiple identity columns in Code First? Because of clustering

  • 0

a.k.a How can we create multiple identity columns in Code First?

Because of clustering performance, a common recommendation is to use an autoincremented integer column instead of a GUID created with newid().

In order to declare a column as autoincrement, you have to specify it with the Annotation [DatabaseGenerated(DatabaseGeneratedOption.Identity)].

But, you can only have one identity in a table.

So starting with a base model like:

public abstract class ModelBase {
    // the primary key
    public virtual Guid Id { get; set; }

    // a unique autoincrementing key
    public virtual int ClusterId { get; set; }
}

how do we set it up so that:

  1. Guid is automatically generated by the database, not code
  2. ClusterId is autoincremented
  3. Entity Framework Code First doesn’t throw all sorts of errors like:
    • Modifications to tables where a primary key column has property ‘StoreGeneratedPattern’ set to ‘Computed’ are not supported. Use ‘Identity’ pattern instead.

FYI, if you do want to automatically generate it in code, you could skip the annotation on the Id field and do something like:

public abstract class AbstractContext : DbContext {

  /// <summary>
  /// Custom processing when saving entities in changetracker
  /// </summary>
  /// <returns></returns>
  public override int SaveChanges()
  {
      // recommended to explicitly set New Guid for appropriate entities -- http://msdn.microsoft.com/en-us/library/dd283139.aspx
      foreach (var entry in ChangeTracker.Entries<ModelBase>().Where(e => e.State == EntityState.Added) ) {

          // only generate if property isn't identity...
          Type t = entry.Entity.GetType();
          var info = t.GetProperty("Id").GetCustomAttributes(
              typeof(DatabaseGeneratedAttribute), true).Cast<DatabaseGeneratedAttribute>().Single();

          if (info.DatabaseGeneratedOption != DatabaseGeneratedOption.Identity) {
              entry.Entity.Id = Guid.NewGuid(); // now we make it
          }
      }
      return base.SaveChanges();
  }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T22:04:37+00:00Added an answer on June 9, 2026 at 10:04 pm

    This ended up working for me, Entity Framework 5.

    1. Turn off automatic migrations
    2. Migrate to create the initial table, no frills
    3. Declare the ClusterId as Identity (annotation)

      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public override int ClusterId { get; set; }
      
    4. Migrate

    5. Declare the pk property Id as Identity after the other one has been updated

      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public override Guid Id { get; set; }
      
      • bonus: EF seems to assume Id is primary key, so you don’t need [Key, Required]
    6. Create the migration code like add-migration TrickEfIntoAutogeneratingMultipleColumns

    7. In the Up() method, in the AlterColumn statement, tell the database to autogenerate the GUID by declaring the defaultSqlValue
      • AlterColumn(theTable, "Id", c => c.Guid(nullable: false, identity: true, defaultValueSql: "newid()"));
    8. Migrate

    This seems to “trick” EF, in the sense that it assumes both columns are identities and reacts accordingly. During migration, it tries to make another column an identity, but seemingly doesn’t care when that silently fails — you end up with one marked as Identity and the other with a default value.

    During normal code operation, when EF goes through the SaveChanges/ChangeTracking steps, because it sees the Id property as an Identity it does it’s whole “assign temporary key” thing, so that it’s not trying to use the default 0000000… value, and instead lets the database generate it using the default value function you specified.

    (I would have thought annotating this field as Computed would have accomplished the same thing, but…the errors I mentioned in the question…boo…)

    And, because the ClusterId field is also an Identity in code, and really is an Identity in the database, it autoincrements as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can we create multiple schemas for a particular user? I am currently logged in
How can I create multiple dropdowns in my view with values coming from my
I'll start by quoting google's blog Project owners can now create multiple repositories for
I am building a website templating system where multiple user can create their own
I would like to create a class whose methods can be called from multiple
I was wondering, when you create an object you can often set multiple attributes
I can create and use dynamic two dimensional array in Fortran (in 77 standard).
We need to create a Server with Scala RemotActors, that can handle multiple clients.
How can create a view with mutiple images, something similiar to the photo app
I have created checkboxes. When I selected multiple checkboxes then how can I get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.