Tee See Tee See - 1 year ago 132
C# Question

What can cause an Entity Type has no key defined error besides having no key defined?

I've done extensive research on this error I'm getting:

EntityType 'Todo' has no key defined. Define the key for this EntityType.

Both here and on other websites:

  1. EntityType has no key defined error

  2. https://gilesey.wordpress.com/tag/entitytype-has-no-key-defined/

  3. Entity type has no key defined EF6


However, I am not having the same problem as these askers. The problem that they had is that they failed to identify a primary key in their model, I have not. See below:


namespace TaskMaster.Library.Models
public enum Priority
Highest = 0,
High = 1,
Medium = 2,
Low = 3,
Lowest = 4
public class Todo
public uint Id { get; set; }
public ITask Task { get; set; }
public bool isDone { get; set; }
public bool isVisible { get; set; }

public Priority Priority { get; set; }



namespace TaskMaster.Library.Models
class TodoContext : DbContext
public TodoContext()
:base ("DefaultConnection")
this.Configuration.LazyLoadingEnabled = false;

public virtual DbSet<Todo> Todos { get; set; }


What I've tried (besides reading the articles):

  1. Changing the Id name to TodoId.

  2. Adding [Key]

  3. Double checking the DbSet and DBContext

However, EF is clearly recognizing this model as noted by the error message itself; if it was not, it would not be displaying the model name in the error.

My only wonder is could he enum definition or having a uint id have to do with it? It's outside the class though so I don't see how. Thanks. I'm using EF 6.1.3.

Pat Pat
Answer Source

The Entity Framework doesn't support properties (key or otherwise) of type uint. Try this experiment:

Change your Todo.Id property to type int (or long). It will work fine.

Add a non-key property of type uint. It will still scaffold your migration successfully, but it will completely ignore the uint property, just as if you had excluded it explicitly.

There's a suggestion to add support for uint at Support unisgned integer, and the EF team says it will be supported in EF7.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download