2

My problem: Using Entity Framework with MySQL, I kept getting the error "BLOB/TEXT used in key specification without a key length". I added [StringLength(20)] and [MaxLength(20)] and [Column(TypeName="nvarchar")] to my only string-type [Key], with no progress.

I've found other references to this problem, but they are all out-of-date; there were issues in Connector/Net a few years ago related to this which have since been fixed.

2
  • I found this question while searching for how to get a MySql TEXT column generated at all with Code First. Could you post some sample code? Commented Apr 28, 2017 at 20:09
  • The solution for me was changing column type from text to varchar(64). I posted an actual answer but we have trigger happy mods deleting them. Commented Jul 5, 2021 at 0:03

3 Answers 3

2

The eventual solution: It was not a [Key] problem at all, despite the error message.

I had used [Index] on some columns which were string-types too, and unlike columns with the [Key] attribute, EF does not automagically set these to an nvarchar type of limited length. Decorate those columns with the attribute [MaxLength(255)], and MySQL is happy.

Sign up to request clarification or add additional context in comments.

Comments

0

The OP's solution of setting [MaxLength(255)] attribute does not seem to work with EF Core. I was able to fix the problem by modifying column type from text to varchar(255).

Comments

0

I don't have enough reputation to comment, but the answer given by Ollyver also worked for me with EF6. Adding [MaxLength(255)] fixed the issue. Adding MaxLength also changed the column type from longtext (which was the default) to varchar(255).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.