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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:45:18+00:00 2026-06-10T02:45:18+00:00

I started a project using Entity Framework 4.3 Code First with manual migrations and

  • 0

I started a project using Entity Framework 4.3 Code First with manual migrations and SQL Express 2008 and recently updated to EF5 (in VS 2010) and noticed that now when I change something like a foreign key constraint, the migrations code adds the “dbo.” to the start of the table name and hence the foreign key name it constructs is incorrect for existing constraints (and in general now seem oddly named).

Original migration script in EF 4.3 (note ForeignKey(“Products”, t => t.Product_Id)):

    CreateTable(
        "Products",
        c => new
            {
                Id = c.Int(nullable: false, identity: true),
                ProductName = c.String(),
            })
        .PrimaryKey(t => t.Id);

    CreateTable(
        "KitComponents",
        c => new
            {
                Id = c.Int(nullable: false, identity: true),
                Component_Id = c.Int(),
                Product_Id = c.Int(),
            })
        .PrimaryKey(t => t.Id)
        .ForeignKey("Products", t => t.Component_Id)
        .ForeignKey("Products", t => t.Product_Id)
        .Index(t => t.Component_Id)
        .Index(t => t.Product_Id);

Foreign Key names generated:
FK_KitComponents_Products_Product_Id
FK_KitComponents_Products_Component_Id

If I then upgrade to EF5 and change the foreign key the migration code looks something like (note the “dbo.KitComponents” and “dbo.Products” as opposed to just “KitComponents” and “Products”):

DropForeignKey("dbo.KitComponents", "Product_Id", "dbo.Products");
DropIndex("dbo.KitComponents", new[] { "Product_Id" });

and the update-database fails with the message:
‘FK_dbo.KitComponents_dbo.Products_Product_Id‘ is not a constraint.
Could not drop constraint. See previous errors.

so it seems as of EF5 the constraint naming has changed from
FK_KitComponents_Products_Product_Id
to
FK_dbo.KitComponents_dbo.Products_Product_Id (with dbo. prefix)

How can I get EF5 to behave as it was in EF 4.3 so I don’t have to alter every piece of new migration code it spits out?

I haven’t been able to find any release notes about why this changed and what to do about it 🙁

  • 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-10T02:45:19+00:00Added an answer on June 10, 2026 at 2:45 am

    You can customize the generated code by sub-classing the CSharpMigrationCodeGenerator class:

    class MyCodeGenerator : CSharpMigrationCodeGenerator
    {
        protected override void Generate(
            DropIndexOperation dropIndexOperation, IndentedTextWriter writer)
        {
            dropIndexOperation.Table = StripDbo(dropIndexOperation.Table);
    
            base.Generate(dropIndexOperation, writer);
        }
    
        // TODO: Override other Generate overloads that involve table names
    
        private string StripDbo(string table)
        {
            if (table.StartsWith("dbo."))
            {
                return table.Substring(4);
            }
    
            return table;
        }
    }
    

    Then set it in your migrations configuration class:

    class Configuration : DbMigrationsConfiguration<MyContext>
    {
        public Configuration()
        {
            CodeGenerator = new MyCodeGenerator();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've recently started using Entity Framework migrations and noticed that the database name is
I'm using Entity Framework 4 with the Model First approach. I started the project,
I started an ASP.NET project with Entity Framework 4 for my DAL, using SQL
I just started using Hibernate on a new project, and with my first entity,
I started using Entity Framework (Using POCO Classes) for a MVC 3 C# project
Hi all I've just started a new project using Visual Web Developer 2008 Express
At work, we recently started a project using CouchDB (a document-oriented database). I've been
I have a recently started project where i'm using mongoid for the database and
I have started using Linq to SQL for a project im working on and
I have started to look at the Entity Framework for a project I am

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.