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 8987559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:49:32+00:00 2026-06-15T21:49:32+00:00

I have a scenario where i would like to change the primary key name

  • 0

I have a scenario where i would like to change the primary key name in an entity and be able to run update-database -force. See below for code and error getting when i try.

Entity was:

public class Team
{
    [Key]
    [HiddenInput(DisplayValue = false)]
    public virtual int Id { get; set; }

    [Display(Name = "Full Name:")]
    public virtual string Name { get; set; }
}

Entity Changed to:

public class Team
{
    [Key]
    [HiddenInput(DisplayValue = false)]
    public virtual int TeamId { get; set; }

    [Display(Name = "Full Name:")]
    public virtual string Name { get; set; }
}

When i run Update-database -Force i get the following error.

Multiple identity columns specified for table 'Teams'. Only one identity column per table is allowed.

Its a matter of naming convention and i need this to be TeamId when i reference it latter, simply Id conflicts with child entity classes.

Any ideas on how i can do this successfully?

  • 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-15T21:49:33+00:00Added an answer on June 15, 2026 at 9:49 pm

    Depends on the version of EF you are using.
    Even with migrations the result that you will see is something like:

    “drop column Id ” and “add column TeamId”.

    With this you will lose all values and “child connections”……

    The only “secure” solution I am seeing at this point is a mix of Migrations and “hand SQL operations”.

    EASY solution:

    1- taking in consideration that you already have a “base” migration creating the table with ID, now create the new migration with the “update”. Now do NOT run it yet.

    2- Open that file and write a new line BEFORE the generated lines and use a SQL command, something like this:

         SQL("ALTER TABLE table_name RENAME COLUMN old_name to new_name;");
    

    This will change the name BEFORE the migration deletes the column and create a new one, what will happen is: you change the name before the delete, then the delete is executed but it will “fail” but it will not hurt nothing.

    But now you ask: why do I do this? well if you are using migrations even if you delete the lines to delete the column and create a new one, the next time you create automatically a new migration file this new lines will be there…… this is why.

    UPDATED ANSWERS #1

    When I talk about Entity Framework Migrations I am referring to this:
    http://blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-code-based-migrations-walkthrough.aspx
    When you Run the ‘Add-Migration AddBlogUrl’ command in Package Manager Console, a new file (*.cs) is created.

    Example of this file migration file with SQL commands:

    public partial class AddAbsencesTypesAndCategories : DbMigration
        {
            public override void Up()
            {
                CreateTable(
                    "pvw_AbsenceType",
                    c => new
                        {
                            Id = c.Int(nullable: false, identity: true),
                            Name = c.String(nullable: false),
                            CountAsVacation = c.Boolean(nullable: false),
                            IsIncremental = c.Boolean(nullable: false),
                        })
                    .PrimaryKey(t => t.Id);
    
              .....
    
                AddColumn("pvw_Absence", "CategoryId", c => c.Int(nullable: false));
                            AddForeignKey("pvw_Absence", "StatusId", "pvw_AbsenceStatusType", "Id");
                AddForeignKey("pvw_Absence", "CategoryId", "pvw_AbsenceType", "Id");
                CreateIndex("pvw_Absence", "StatusId");
                CreateIndex("pvw_Absence", "CategoryId");
                DropColumn("pvw_Absence", "MainCategoryId");
                DropColumn("pvw_Absence", "SubCategoryId");
               ......
                Sql(@"
                                            SET IDENTITY_INSERT [dbo].[pvw_AbsenceStatusType] ON
                        INSERT pvw_AbsenceStatusType (Id, Name) VALUES (1, N'Entwurf')                       
                                            SET IDENTITY_INSERT [dbo].[pvw_AbsenceStatusType] OFF
                ");    
                .....
            }
    
            public override void Down()
            {
                ........
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this scenario where I would like to redirect my domains using the
I have an architecture scenario and I would like to discuss to get your
I have a performance sensitive scenario where I would like to write UTF-8 to
I would like to present a scenario where it would be nice to: have
I have a pretty specific scenario where I would like to select all elements
I have an application server which connects to a database server. I would like
Here is my scenario: I have a document, currently a FlowDocument, that I would
I have scenario, I have two update panels on the page (both have update
we have a classic JSP + Servlets application and would like to show Content
I have a problem that I would like to share. The context is a

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.