Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I stop Entity Framework 5 migrations adding dbo. into key names?

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 :(

like image 331
mips Avatar asked Aug 21 '12 11:08

mips


People also ask

How do I get rid of migrations in Entity Framework?

Delete your Migrations folder. Create a new migration and generate a SQL script for it. In your database, delete all rows from the migrations history table. Insert a single row into the migrations history, to record that the first migration has already been applied, since your tables are already there.

What does add migration do?

Add-Migration: Creates a new migration class as per specified name with the Up() and Down() methods. Update-Database: Executes the last migration file created by the Add-Migration command and applies changes to the database schema.


2 Answers

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();     } } 
like image 63
bricelam Avatar answered Sep 23 '22 13:09

bricelam


For Automatic Migrations use this code:

public class MyOwnMySqlMigrationSqlGenerator : MySqlMigrationSqlGenerator {     protected override MigrationStatement Generate(AddForeignKeyOperation addForeignKeyOperation)     {         addForeignKeyOperation.PrincipalTable = addForeignKeyOperation.PrincipalTable.Replace("dbo.", "");         addForeignKeyOperation.DependentTable = addForeignKeyOperation.DependentTable.Replace("dbo.", "");         MigrationStatement ms = base.Generate(addForeignKeyOperation);         return ms;     } } 

And Set it on configuration:

SetSqlGenerator("MySql.Data.MySqlClient", new MyOwnMySqlMigrationSqlGenerator()); 
like image 26
Danilo Breda Avatar answered Sep 24 '22 13:09

Danilo Breda