Eagletrophy Eagletrophy - 7 months ago 104
SQL Question

asp.net mvc 5.1 mysql Entity Framework Controller Generation

I have a asp.net mvc 5.1 solution with 3 projects in it shown by the image below:

Folder structure of my solution in visual studio

I have Ninject, FakeitEasy, MySql nuget packages installed in my project and I am using the repository pattern in my project so I have an IRepository generic interface, IDbContext interface and a concrete repository generic class (all this is in the AccountManager.Domain).
With everything Ninject and mysql all setup, I built the solution and so far so good until I tried to add a controller using the scaffolding options given with Entity Framework and I get an error saying:


Unable to retrieve metadata for 'AccountManager.Domain.Entities.Bank' using the same DbCompiledModel to create contexts against different types of Database servers is not supported. Instead create a separate DbCompiledModel for each type of server being used.


I saw this when I was head banging with sqlite and EF code first. What does it mean again? Anyone see some perspective I don't?

Answer

I have been plugging away at this annoyance for a while and I found that its something that is just simple. Decorating my context class with this mysqlefconfig like so:

[DbConfigurationType(typeof(MySqlEFConfiguration))]

helps the context to make the necessary adjustments to the migration table EF creates automatically so it works with MySql is the reason why my controllers didn't scaffold at all. I got the idea to try comment this out from http://stackoverflow.com/users/3671905/yurko and the scaffolding worked gorgeously.

Only thing to note is that after you finished your scaffolding you need to uncomment it so that all the MySQL peculiarities required by EF should be restored. I prefer attribute configuration than creating files and editing infrastructure files.