[{"data":1,"prerenderedAt":96},["ShallowReactive",2],{"/2025/11/23-ef6-mysql-migrations-error-specified-key-was-too-long-max-key-length-is-767-bytes":3},{"id":4,"title":5,"body":6,"date":87,"description":72,"extension":88,"meta":89,"navigation":90,"path":91,"robots":92,"seo":93,"stem":94,"__hash__":95},"posts/2025/11/23 EF6-MySql-migrations-error-specified-key-was-too-long-max-key-length-is-767-bytes.md","23 EF6 MySql Migrations Error Specified Key Was Too Long Max Key Length Is 767 Bytes",{"type":7,"value":8,"toc":84},"minimark",[9,18,25,28,31,33,36,38,41,54,56,59,73,76],[10,11,13],"post-title",{":date":12},"date",[14,15,17],"h1",{"id":16},"ef6-mysql-migrations-error-specified-key-was-too-long-max-key-length-is-767-bytes","EF6 MySql Migrations Error \"Specified key was too long; max key length is 767 bytes\"",[19,20,21],"notes",{},[22,23,24],"p",{},"This is a repost from my old blog. First posted in 3/18/2020.",[26,27],"br",{},[22,29,30],{},"We were in the middle of moving to MySQL and would like to use EF Migrations against MySQL. But the error held us back a little.",[26,32],{},[22,34,35],{},"When trying to apply EF Migrations, it failed with \"Specified key was too long; max key length is 767 bytes\" error.",[26,37],{},[22,39,40],{},"The following article helped solve my problem.",[22,42,43],{},[44,45,48],"span",{"className":46},[47],"text-blue-600",[49,50,51],"a",{"href":51,"rel":52},"https://docs.microsoft.com/en-us/aspnet/identity/overview/getting-started/aspnet-identity-using-mysql-storage-with-an-entityframework-mysql-provider#adding-custom-migrationhistory-context",[53],"nofollow",[26,55],{},[22,57,58],{},"Under \"Adding custom MigrationHistory context\" section, it talks about creating a custom HistoryContext class. In my case:",[60,61,62],"code-block",{},[63,64,69],"pre",{"className":65,"code":67,"language":68},[66],"language-text","Public Class MySqlHistoryContext\n        Inherits HistoryContext\n\n    Sub New(existingConnection As DbConnection, defaultSchema As String)\n        MyBase.New(existingConnection, defaultSchema)\n    End Sub\n\n    Protected Overrides Sub OnModelCreating(modelBuilder As DbModelBuilder)\n        MyBase.OnModelCreating(modelBuilder)\n        modelBuilder.Entity(Of HistoryRow)().Property(Function(h) h.MigrationId).HasMaxLength(100).IsRequired()\n        modelBuilder.Entity(Of HistoryRow)().Property(Function(h) h.ContextKey).HasMaxLength(200).IsRequired()\n    End Sub\nEnd Class\n","text",[70,71,67],"code",{"__ignoreMap":72},"",[22,74,75],{},"Then use it on the generated Configuration file. My problem is fixed afterwards.",[60,77,78],{},[63,79,82],{"className":80,"code":81,"language":68},[66],"Friend NotInheritable Class Configuration\n        Inherits DbMigrationsConfiguration(Of MyDbContext)\n\n    Public Sub New()\n        AutomaticMigrationsEnabled = False\n\n        SetHistoryContextFactory(MySqlProviderInvariantName.ProviderName, Function(connection, schema) New MySqlHistoryContext(connection, schema))\n    End Sub\n\n    Protected Overrides Sub Seed(context As MyDbContext)\n    End Sub\n\nEnd Class\n",[70,83,81],{"__ignoreMap":72},{"title":72,"searchDepth":85,"depth":85,"links":86},2,[],"2025-11-23T00:00:00.000Z","md",{},true,"/2025/11/23-ef6-mysql-migrations-error-specified-key-was-too-long-max-key-length-is-767-bytes",null,{"title":5,"description":72},"2025/11/23 EF6-MySql-migrations-error-specified-key-was-too-long-max-key-length-is-767-bytes","H3DOBGBRYPhX5JjMENwQc-fRPUDLuPnus6B0y9pSTLI",1785167452441]