.net core tutorials

How to reverse engineer MySql Database EF Core

There are many instances in software development projects when there will be a requirement to reverse engineer an existing database, in order to continue with a development project.EF Core has made the task of reverese engineering existing databases really easy. Although I had some issues with MySql which I thought I would help clarify just in case there were others with the same issues.

In this post I will provide details on how to reverse engineer a MySql database using EF Core.

I have recently engaged on a new project for a client, who had a simple Candidate registration application, developed using PHP and the Laravel Framework with a MySql database backend.The client wanted to port the application to C# and .net Core. However, there intention is first to develop a .net core Web Api and to deploy this to Amazon AWS.

I won’t go into the reasons why the client wanted to do this type of technological and application refresh, as that is beyond the scope of this article. Suffice to say either technology is wqell suited for this task, however the client has there reasons and a business case.



Reverse Engineering a MySql Database

Microsoft Entity Framework core has made it really easy to Reverse Engineer existing databases, which are now included optional libraries available as nuget packages.

In previous versions of Entity Framework, reverse engineering was available via the Entity Framework Power Tools, and as far as I can recall, I don’t think there was on option to do any other databases except SQL. ALthough I have to be honest, during those years I was predominantly focused on MS SQL, so this statement my not be 100%.

Although the process of Reverse Engineering MySQL databases, is ultimately a trivial process, I did have some issues. I have to preface this with, it could quite possibly be my Development Environment, could be an anomoly compared to others.

I specifically use Jetbrains Rider 2018.2 on Ubuntu 18.04 with .net Core 2.1 installed.



How to reverse MySQL database

I created a .net core Class Library Project, making use of the default template with DotnetCore CLI –

The usual process would be add the Nuget Package References:

  • Microsoft.EntityFrameworkCore.Tools
  • MySql.Data.EntityFrameworkCore.Design
  • MySql.Data.EntityFrameworkCore

Then simply use the terminal command in your Project Directory

The issue for me was that this didn’t work and I received the following error

It seems that the Official MySql nuget package doesn’t have the necessary method implemented.

Fortunately, conducting a search of avaiable nuget packages I was able to find suitable packages, I found the Pomelo Foundatiion – Pomelo.EntityFrameworkCore.MySql which worked.

All I needed to do was import the packages

  • Microsoft.EntityFrameworkCore.Tools
  • Pomelo.EntityFrameworkCore.MySql.Design
  • Pomelo.EntityFrameworkCore.MySql

Pomelo-mysql

I was then able to use the Terminal Command:

My Database Context and Entities were successfully generated.



Conclusion

This is a great start and drastically reduces the effort.

Although the generated code can be used as it – subject to you addressing some the warnings which a clearly labeled within the generated DbContext, specifically regarding removing the hard coded connection string.

I found my OCD kicking in and I had to do some tidying up and re-organising the generated code, but for the most part the generated code works and simplifies your process.

Gary Woodfine

Freelance Full Stack Developer at threenine.co.uk
Helps businesses by improving their technical proficiencies and eliminating waste from the software development pipelines.

A unique background as business owner, marketing, software development and business development ensures that he can offer the optimum business consultancy services across a wide spectrum of business challenges.

Latest posts by Gary Woodfine (see all)

Affiliate Disclaimer

Disclosure: Please note that some of the links included in the blog posts are affiliate links, which means I will earn a commission if you decide to make a purchase.

I only include affiliate links to products, services and companies that I have personal experience and have actually used. I only recommend them because they are helpful and useful, not because of the small commissions I make if you decide to buy something.

Please do not spend any money on these products unless you feel they are relevant, suitable and will help you achieve your goals.

Buying anyone of these products and the commisions earned will not make me an overnite multi millionaire, but they will help to cover the hosting costs and compensate for the countless hours and effort I put in to create meaningful and valuable free content I provide to help others succeed.

You've also undoubtedly noticed that I also use various advertising networks - unless of of course you're using an Ad blocker, this is also an attempt to reduce my monthly deficit I experience in providing free relevant, valuable and on occassion insightful content for the benefit of others.

I only really make some money if people click on the ads, unless of course you consider 1c per 1000 impressions real money!

Bear in mind just how many impressions I need to make before I can cover my £20 hosting costs a month!

If you are using an adblocker and guilt stricken you can of course donate using any of the buttons below, but I won't be holding my breath.

Buy me a coffeeBuy me a coffee

π