I'm a PHP/MySQL developer trying to move over to other technologies like .NET Core. I'm not sure if I like the idea of switching to SQL Server over MySQL (due to licensing), so I've been trying to get .NET Core to play nicely with MySQL.
I've set up a fairly basic .NET Core 2.0 project using Identity as the authentication method and everything was working fine while the DB driver was SqlLite
with a local database. I tried switching to a MySQL
database and ran my migrations and they failed.
I received the error: Specified key was too long; max key length is 3072 bytes
when trying to run the following table-creation query:
CREATE TABLE `AspNetUserTokens` (
`UserId` varchar(767) NOT NULL,
`LoginProvider` varchar(767) NOT NULL,
`Name` varchar(767) NOT NULL,
`Value` text NULL,
PRIMARY KEY (`UserId`, `LoginProvider`, `Name`)
);
My Mysql database uses a character set of UTF8mb4
so it cannot use the primary key specified above which works out to be around 9204 bytes.
How can I customize the way that the default Identity tables are created? If possible, I would like to replace the huge key with a more sane primary key on this table that is just an auto increment
integer.
Is there any way to customize how the default Identity related tables are created without manually hacking at the migration files. Is there any way I can do something in the OnModelCreating
method of my ApplicationDbContext
? Should I stop fighting with MySQL and succumb to MS SQL?
Edit
I was able to workaround the issue by using the Pomelo MySQL Driver rather than Oracle. The Pomelo driver seems to default string fields to max length 127 characters which allows the above key to fit in the 3072 byte limit. However I would still be interested in knowing how to fine-tune and customize the base Identity tables provided in the .NETCore Scaffolding.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…