由於 MySQL Innodb 引擎表索引字段長度的限制為 767 字節,因此對於多字節字符集的大字段(或者多字段組合索引),創建索引會出現上面的錯誤。
以 utf8mb4 字符集 字符串類型字段為例:utf8mb4 是 4 字節字符集,則默認支持的索引字段最大長度是: 767 字節 / 4 字節每字符 = 191 字符,因此在 varchar(255) 或 char(255) 類型字段上創建索引會失敗。
解決方法:
1.使用NuGet引用MySQL.Data.Entities。
2.在你的DbContext里添加代碼:
[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))] public class MyContext : DbContext { public MyContext() : this() { } static MyContext () { DbConfiguration.SetConfiguration(new MySql.Data.Entity.MySqlEFConfiguration()); } }
完。
