Entity Framework with mysql, table capitalization issue between linux and windows

We are currently developing a product using the Code First Entity Framework and Mysql. The development database is hosted on Windows, while production mysql is on Linux.

The problem I ran into is that the tables in mysql are called like this:

mydatabase.industry mydatabase.account ... 

Entity framework creates the following request:

  Select * FROM mydatabase.Industry; 

Pay attention to the capital letter. This works fine on mysql on Windows, but on Linux I get this error:

  Table 'mydatabase.Industry' doesn't exist 

Any ideas?

+6
source share
3 answers

Entity Framework will use the same name (capitalization, etc.) as was announced for the object. So, for example, if you declare a model object as:

 public class Industry { public int IndustryID { get; set; } } 

Entity Framework will look for an industry table with an IndustryID column.

You can change this by adding annotations to your models. Follow these steps:

 [Table("industry")] public class Industry { public int IndustryID { get; set; } } 

That way, your objects will still use the appropriate .NET naming scheme, but it will match your respective database. You can also change the name of colunns using ColumnAttribute .

Alternatively, you can change the table names in MySQL.

+6
source

Entity framework creates the following request:

Select * FROM mydatabase.Industry; Pay attention to the capital letter. This works fine on mysql on Windows, but on Linux I get this error:

Table 'mydatabase.Industry' does not exist Any ideas?

This has nothing to do with the structure of the entity itself.
This is a variation of the classic default configuration in two different OS issues.

You may not need a solution, but here is the cause of your problem:

MySQL stores tables as files.
And MySQL was originally developed on Linux, where file names and therefore table names were case-sensitive (since most Linux file systems are case-sensitive).

MySQL was then ported to Windows, where the file system is not case sensitive.

In windows, the file system is not case sensitive and therefore cannot distinguish between lower and upper case files.

To fix the problems, you must add an option to ignore the case of table names.

Thus, the case-senstivity identifier configuration variable "lower_case_table_names" was created, with which you can switch these parameters.

Further information on the possible values โ€‹โ€‹of [0, 1, 2] is here: http://dev.mysql.com/doc/refman/5.0/en/identifier-case-sensitivity.html

On Windows, you can configure it in the my.ini file found in or around:

 C:\Program Files\MySQL\MySQL Server XY 

depending on the version used. If you do not find this option, you can simply add it to the end of the my.ini file.

On Linux, this configuration file is usually located in /etc/my.cnf or /etc/mysql/my.cnf but this may vary between Linux versions and distributions.

The default value for this value is

 on Linux: Case-Senstivie on Windows: Case-Insensitive 

Thus, the framework entity generated SQL, which was generated from the model with the wrong shell, works on Windows, but not on Linux.

All you have to do is change the value of lower_case_table_names on Linux to 1 in the my.cnf file so that it is not case sensitive.

Please note that although you can also make MySQL on Windows case-sensitive, this is not a good idea.

Then, remember to restart the MySQL / mysqld service before checking to see if it works.

In all cases, always remember whether it works on Linux, since the behavior of MySQL is not always the same on these two systems.

The reason for the different defaults is that on Linux you have better performance when you are in case sensitive mode, because then you do not always have lowercase names. On Windows, on the other hand, it is better to have a case insensitive configuration, because you cannot have two files with the same name that differ only in the case in the same folder. This will cause a malfunction. Thus, the default value is not case sensitive in Windows.

By the way, this is why the performance of MySQL on Linux is better than the performance on Windows (among other things, for example, better planning and a faster IO / file system on Linux)

Please note that you can also change the case sensitivity of queries (string comparison) by doing

 CREATE DATABASE test_database CHARACTER SET utf8 COLLATE utf8_general_cs; 

or

 CREATE DATABASE test_database CHARACTER SET utf8 COLLATE utf8_general_ci; 

respectively

And you can also set case sensitivity on one table, for example

 DROP TABLE IF EXISTS single_test_table; CREATE TABLE single_test_table( single_test_id int unsigned NOT NULL auto_increment, ... PRIMARY KEY PK_single_test_id (single_test_id ), ... ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE utf8_general_cs; 
+3
source

Here is my answer, taken from the comments:

usr: Why don't you change the configured table name to lowercase? The SQL table name and object name are independently configured.

AFrieze: Do you mean changing the name of an object to lowercase? So instead of this class C # "Industry" Will I have a class "industry"?

usr: No, they are different. You can give the table name xyz the entity Abc. - usr is now editing

+1
source

Source: https://habr.com/ru/post/909351/


All Articles