Ef Core Charset Problem

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for MySQL
Post Reply
cangunaydin
Posts: 4
Joined: Tue 25 Apr 2017 22:34

Ef Core Charset Problem

Post by cangunaydin » Mon 24 Jul 2017 07:38

Hello i am using ef core with .net framework 4.6.1
I am trying to use devart mysql provider.
Everything works fine when i did the configuration in my dbcontext. But there is a problem with the charset i suppose. When i run my seed methods. There is an if condition for the default languages that i am adding to my languages table. But even the values are the same. It is always adding the values to my database.
to be more specific i will share my code and connection string.

Code: Select all

"ConnectionStrings": {
    "Default": "server=***;database=BookAndAdDb;port=3306;uid=****;password=****;CharSet=utf8;"
  }
and this is the seed class that add the languages to db.

Code: Select all

using System.Collections.Generic;
using System.Linq;
using Abp.Localization;
using ByteBrick.BookAndAd.EntityFrameworkCore;

namespace ByteBrick.BookAndAd.Migrations.Seed.Host
{
    public class DefaultLanguagesCreator
    {
        public static List<ApplicationLanguage> InitialLanguages => GetInitialLanguages();

        private readonly BookAndAdDbContext _context;

        private static List<ApplicationLanguage> GetInitialLanguages()
        {
            return new List<ApplicationLanguage>
            {
                new ApplicationLanguage(null, "en", "English", "famfamfam-flags gb"),
                new ApplicationLanguage(null, "ar", "العربية", "famfamfam-flags sa"),
                new ApplicationLanguage(null, "de", "German", "famfamfam-flags de"),
                new ApplicationLanguage(null, "it", "Italiano", "famfamfam-flags it"),
                new ApplicationLanguage(null, "fr", "Français", "famfamfam-flags fr"),
                new ApplicationLanguage(null, "pt-BR", "Portuguese", "famfamfam-flags br"),
                new ApplicationLanguage(null, "tr", "Türkçe", "famfamfam-flags tr"),
                new ApplicationLanguage(null, "ru", "Русский", "famfamfam-flags ru"),
                new ApplicationLanguage(null, "zh-CN", "简体中文", "famfamfam-flags cn"),
                new ApplicationLanguage(null, "es-MX", "Español México", "famfamfam-flags mx")
            };
        }

        public DefaultLanguagesCreator(BookAndAdDbContext context)
        {
            _context = context;
        }

        public void Create()
        {
            CreateLanguages();
        }

        private void CreateLanguages()
        {
            foreach (var language in InitialLanguages)
            {
                AddLanguageIfNotExists(language);
            }
        }

        private void AddLanguageIfNotExists(ApplicationLanguage language)
        {
            if (_context.Languages.Any(l => l.TenantId == language.TenantId && l.Name == language.Name))
            {
                return;
            }

            _context.Languages.Add(language);

            _context.SaveChanges();
        }
    }
}
this line never returns. always adding the languages again and again to my db.

Code: Select all

if (_context.Languages.Any(l => l.TenantId == language.TenantId && l.Name == language.Name))
            {
                return;
            }
i have tried unicode=true in my connection string and i have tried the charset utf8mb4 but no luck with it.
my default collation is "utf8-default collation" in mysql db.

Any help will be appreciated.
Thanks in Advance.
CAN

cangunaydin
Posts: 4
Joined: Tue 25 Apr 2017 22:34

Re: Ef Core Charset Problem

Post by cangunaydin » Mon 24 Jul 2017 12:34

More is coming up.
I tought that it is related with charset but maybe it is not. It can be a problem with where clauses and any clauses.

when i write this query instead it is working fine, when i debug this code

Code: Select all

private void AddLanguageIfNotExists(ApplicationLanguage language)
        {
            var firstRecord = _context.Languages.Where(l => l.Id==1).First();
            var isEqual=language.Name.Equals(firstRecord.Name);
            var isTenantSame = language.TenantId == firstRecord.TenantId;
            if (_context.Languages.Any(l => l.TenantId == language.TenantId && l.Name.Equals(language.Name)))
            {
                return;
            }

            _context.Languages.Add(language);

            _context.SaveChanges();
        }

isEqual and isTenantSame variables are true, which probably means that sth is wrong in any clause, i have also tried it with where clause it brings 0 row(s). Any idea about this?

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: Ef Core Charset Problem

Post by Shalex » Thu 27 Jul 2017 11:10

We cannot reproduce the issue with the following code:

Code: Select all

    //CREATE DATABASE db_utf8
    //    CHARACTER SET utf8
    //    COLLATE utf8_general_ci;

    //CREATE TABLE Languages(
    //    TenantId INT(11) NOT NULL AUTO_INCREMENT,
    //    Name VARCHAR(50) DEFAULT NULL,
    //    PRIMARY KEY(TenantId)
    //);

    var monitor = new Devart.Data.MySql.MySqlMonitor() { IsActive = true };

    using (var context = new db_utf8Model())
    {
        var language = new Language() { TenantId = 1, Name = "$" };
        context.Languages.Add(language);
        context.SaveChanges();

        using (var _context = new db_utf8Model())
        {
            if (_context.Languages.Any(l => l.TenantId == language.TenantId && l.Name == language.Name))
            {
                Console.WriteLine("success");
            }
            else throw new Exception("error");
        }
    }
    Console.ReadKey();
Please create a small complete test project with the corresponding DDL/DML script for reproducing and upload it to our FTP server: ftp://ftp.devart.com (anomymous / yourEmail).

Post Reply