Implementing dotnet ef database update command in MySql
October 13, 2016 by Anuraj
C# ASP.NET Core MySql EF Migrations
This post is about implementing dotnet ef database update command with MySql connector (MySql.Data.EntityFrameworkCore). Few days back I did a post on using MySql in ASP.NET Core. But one problem I found was when calling dotnet ef database update command, the lib was throwing not implemented exception. I had a discussion with @RuAnt, and I found the github repo - https://github.com/mysql/mysql-connector-net. As I got some time today, I thought of exploring the source and implementing the same, if it is not complex. :) So as a first step, I looked into the Sqlite and SqlServer implementations, and from the exception stack, I come to know about the method, which is not implemented.
It was ExistsSql
property in the MySQLHistoryRepository.cs class. I looked the implementation in SqlServer and I found it is looking for existing tables, using SELECT OBJECTID
. Then I searched how I can do same with MySql, something like this.
And I implemented same in C# something like this.
Also I implemented one more method, InterpretExistsResult
which return bool based on the value from ExistsSql
(based on my assumptions), which is like this.
I found the database update working most of the scenarios, except when your model class has DateTime type. When you’re using DateTime EF Code migrations are generating a default value, which is causing problem while executing the database update. To make this work, you need to open the migrations file and remove the default DateTime value.
And here is the database update
command execution, after implementation.
Yes there are some warnings I am getting related to the MySql.Data package, but right now I am ignoring that.
Happy Programming :)
Copyright © 2024 Anuraj. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Powered by Jekyll. Hosted with ❤ by GitHub