tabs ↹ over ␣ ␣ ␣ spaces

by Jiří {x2} Činčura

Hunting “EntityType ’Image’ has no key defined. Define the key for this EntityType." problem

10 May 2013 2 mins Entity Framework, Firebird

Few weeks back I was hired to debug the above mentioned problem. The application was using Entity Framework 5 and Firebird as a database. The message says clearly what’s wrong. How hard can it be to fix the problem, right? I was thinking the same. And how wrong I was.

After receiving the sources and quickly extracting the bare metal with error I started looking for Image type. And there it was. The class had Id property, so the key was kind of there. The default convention should be able to find. Let’s look at configuration, if there’s something suspicious. And even better, the HasKey call is there. OK, maybe there’s some magic in OnModelCreating. Nope. Everything as expected.

By the way, the exact error was:

ModelValidationException: One or more validation errors were detected during model generation:

	System.Data.Entity.Edm.EdmEntityType: : EntityType 'Image' has no key defined. Define the key for this EntityType.
	System.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'Images' is based on type 'Image' that has no keys defined.

That’s the overview. Next two hours I was trying to changes something here and there and make it fail, so I knew it’s doing what it should do. Removing some stuff that might break something. And still no luck. But then something caught my eye. The error is talking about “Image” type. My type from database is “IMAGE” (the mapping and classes were generated by Entity Framework Power Tools and because of how SQL standard defines non-quoted names, it’s by default all uppercase). Yes, there was a property added to some entity that had a type Image, I mean System.Drawing.Image.

So quickly adding Ignore into OnModelCreating and I was done. Lesson learned.

Profile Picture Jiří Činčura is .NET, C# and Firebird expert. He focuses on data and business layers, language constructs, parallelism, databases and performance. For almost two decades he contributes to open-source, i.e. FirebirdClient. He works as a senior software engineer for Microsoft. Frequent speaker and blogger at www.tabsoverspaces.com.