EF6 MySQL changing all table prefixes

ef-code-first entity-framework-6 mysql


I have a mysql db on a server where I've bought some space. Now I want to add a new mvc application to my website to try out some stuff, without buggering up my existing application. That puts me in a bit of a problem though as I only have one database available, I could just buy a new one, but I don't want to have to buy a new one every time I want to try stuff out.

So I figured, why not just pre-fix all my tables with some unique value for that application, that way I can keep the stored data separate while still using the same db. That took me on a journey across countless articles both here on stackoverflow and other places on the web. After about a day I stumbled upon this nugget

<!-- language: c# -->
protected override void OnModelCreating(DbModelBuilder modelBuilder)
        modelBuilder.Types().Configure(entity => entity.ToTable("pf_" + entity.ClrType.Name));

Which does exactly what I want, as long as you either use a mssql database, or you drop your mysql database and create a new one. However, the database I've been given is not an mssql database, and dropping it is not an option.

When, with an existing mysql db, I try to run


I get the following error

Can't find file: '.\mytestdb\dbo@002etestitems.frm' (errno: 2 - No such file or directory)

where "mytestdb" is my local db that I'm testing on, "testitems" is the current/previous tablename, .frm is ofcourse the db format, and I have no idea what this "dbo@002e" is, I tried googling it to no avail.

My stack trace can be found here. http://pastebin.com/yF2dGkm6

Has anyone been in a similar situation, or has ideas as to how I can make it work? =)

3/24/2015 1:49:44 PM

Accepted Answer

dbo@002e stands for "dbo.", looking at your migration file you should discover rows like this one:

RenameTable(name: "dbo.Table", newName: "pf_Table");

Despite everything it's ok with CreateTable with table names prefixed by "dbo.", RenameTable origin the whole "dbo.Name" to be looked for and so the error comes. My suggestion is to remove manually the "dbo." on the RenameTable's "name:" parameter.


After fixed the problem editing the migration file the best solution is to add this to the Configuration constructor:

public Configuration()
    AutomaticMigrationsEnabled = false;
    SetSqlGenerator("MySql.Data.MySqlClient", new MySql.Data.Entity.MySqlMigrationSqlGenerator());
    CodeGenerator = new MySql.Data.Entity.MySqlMigrationCodeGenerator();

It should work since the .Net Connector version 6.7.4 on.

5/15/2015 3:16:32 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow