未加星标

Implementing dotnet ef database update command in MySql

字体大小 | |
[数据库(mysql) 所属分类 数据库(mysql) | 发布者 店小二05 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

Posted by Anuraj on Thursday, October 13, 2016

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.


Implementing dotnet ef database update command in MySql

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.

SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'SCHEMA' AND table_name='TABLE NAME'

And I implemented same in C# something like this.

protected override string ExistsSql { get { var builder = new StringBuilder(); builder.Append("SELECT COUNT(*) FROM information_schema.tables WHERE "); if (TableSchema != null) { builder.Append($"table_schema = '{SqlGenerationHelper.EscapeLiteral(TableSchema)}' AND"); } builder.Append($"table_name='{SqlGenerationHelper.EscapeLiteral(TableName)}'"); return builder.ToString(); } }

Also I implemented one more method, InterpretExistsResult which return bool based on the value from ExistsSql (based on my assumptions), which is like this.

protected override bool InterpretExistsResult(object value) => (long)value != 0L;

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.


Implementing dotnet ef database update command in MySql

Yes there are some warnings I am getting related to the MySql.Data package, but right now I am ignoring that.

Happy Programming :)

Did this post / article help you? Please let me know your feedback in the below comments section. Also, you can ping me on Twitter @anuraj to ask your queries or share the feedback.

本文数据库(mysql)相关术语:navicat for mysql mysql workbench mysql数据库 mysql 存储过程 mysql安装图解 mysql教程 mysql 管理工具

主题: C#SQLMySQLTITwitterCTI
分页:12
转载请注明
本文标题:Implementing dotnet ef database update command in MySql
本站链接:http://www.codesec.net/view/483136.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 数据库(mysql) | 评论(0) | 阅读(70)