您需要使用MaxLength属性.
[MaxLength(16)]
public byte[] test { get; set; }
AddColumn("dbo.testDB", "test", c => c.Binary(storeType: "tinyblob"));
如果需要索引/主键,可以使用属性列并将TypeName设置为“Binary”.
[MaxLength(16), Column(TypeName = "Binary")]
public byte[] test { get; set; }
虽然上面的结果是我的二进制(1)列(这是我如何到达这里).
编辑:要获得正确长度的二进制数组,只需在迁移文件中的二进制后添加(16):
AddColumn("dbo.testDB", "test", c => c.Binary(storeType: "binary(16)"));
不幸的是,添加Column属性的typename不起作用.
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;
}
}