Store an array of bytes using Entity Framework 4, MySQL and code?

Hi, I am trying to keep simple byte[]using EF 4 MySQL (latest connector) and code-first approach.

Simply:

public byte[] Thumbnail {get; set;}

gives the following error on creation:

You have an error in the SQL syntax; check the manual that matches your version of MySQL server for the correct syntax to use near

And then it indicates what happens immediately after the announcement byte[].

Has anyone got any quick tips for me?

+5
source share
1 answer

You need to use the MaxLength attribute.

[MaxLength(16)]
public byte[] test { get; set; }

, tinyblob, / . :

AddColumn("dbo.testDB", "test", c => c.Binary(storeType: "tinyblob"));

column TypeName "Binary", / .

[MaxLength(16), Column(TypeName = "Binary")]
public byte[] test { get; set; }

, (1) ( ).

: , (16) binary :

AddColumn("dbo.testDB", "test", c => c.Binary(storeType: "binary(16)"));

, , Column .

Edit2: , MySqlMigrationSqlGenerator.

internal class CustomMySqlMigrationSqlGenerator : MySqlMigrationSqlGenerator
{
    protected override MigrationStatement Generate(CreateTableOperation op)
    {
        MigrationStatement statement = base.Generate(op);

        foreach (ColumnModel column in op.Columns)
        {
            if (column.MaxLength.HasValue)
            {
                statement.Sql = statement.Sql.Replace($"`{column.Name}` binary", $"`{column.Name}` binary({column.MaxLength.Value})");
            }
        }

        return statement;
    }
}
+2

All Articles