MySQL Connector appending the enum with the value one less than the actual one

元气小坏坏 提交于 2019-12-31 01:47:12

问题


I'm using the MySQL Connector for .NET to manage a MySQL database from C#.

When I try to insert an enum into the database it appends the enum with the value one less than the actual one.

public enum MyEnum {
    FirstValue, SecondValue, ThirdValue;
}

public void InsertEnum() {
    MySqlConnection con = new MySqlConnection(connStr);
    string sql = "INSERT INTO table (Col1) VALUES (@enumVal);";
    MySqlCommand cmd = new MySqlCommand(sql, con);
    cmd.Parameters.AddWithValue("@enumVal", MyEnum.SecondValue);
        //Note I'm trying to insert 'SecondValue' --^

    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();
}

This inserts FirstValue in the database.

The Col1 column, in the database, is of the ENUM datatype:

ENUM('FirstValue','SecondValue','ThirdValue')

If I try to append MyEnum.FirstValue I get a data truncation error (data truncated for column COLUMN_NAME) since it tries to use the same index as .NET (0 in the case of MyEnum.FirstValue), which is a reserved value in MySQL Enums (since they start on 1 by default).

A solution is to append +1 to all enum values or possibly make an extension:

cmd.Parameters.AddWithValue("@enumVal", MyEnum.SecondValue + 1);

It still feels wrong to me that I have to modify the value manually, to a library that is supposed to convert it automatically.

Am I doing something wrong, or is there no default way to deal with this?


回答1:


.NET enumerations are by default zero-based, and backed with an integer (i.e. you can cast a default enumeration to an integer and back).

I imagine what is happening is that the MySQL database driver is casting the enum value to an integer, and trying to insert that.

MySQL treats integers being inserted into enumeration columns as a 1-based index.

I would suggest just making the database column an integer instead of a MySQL enumeration. This way, zero is a valid value.

Alternatively, you could declare the first value of your enumeration to have an integer value of one, to match MySQL, like this:

public enum MyEnum 
{
    FirstValue = 1, 
    SecondValue, 
    ThirdValue
}

I think I'm right in saying that SecondValue and ThirdValue would then be backed with values 2 and 3 respectively.

Then when the connector casts the enumeration to an integer, it will return 1, 2, and 3, which would match what MySQL expects.

The third alternative is to keep the types as-is at both ends, and just make this mapping a function of your data layer (you do have one, right?) - then you only need to deal with this in one place. Could be a little misleading for future developers, mind.




回答2:


You could make your enum like this

public enum MyEnum {
    FirstValue = 1, 
    SecondValue, 
    ThirdValue
}

It will start from 1 instead from 0.



来源:https://stackoverflow.com/questions/13530080/mysql-connector-appending-the-enum-with-the-value-one-less-than-the-actual-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!