【发布时间】:2016-10-27 19:43:16
【问题描述】:
我已经对我遇到的这个错误进行了广泛的研究:
EntityType 'Todo' has no key defined. Define the key for this EntityType.
在这里和其他网站上:
- EntityType has no key defined error
- https://gilesey.wordpress.com/tag/entitytype-has-no-key-defined/
- Entity type has no key defined EF6
等等……
但是,我没有遇到与这些提问者相同的问题。他们遇到的问题是他们未能识别模型中的主键,我没有。见下文:
型号:
namespace TaskMaster.Library.Models
{
public enum Priority
{
Highest = 0,
High = 1,
Medium = 2,
Low = 3,
Lowest = 4
}
public class Todo
{
[Key]
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; }
}
}
我尝试过的(除了阅读文章):
- 将 Id 名称更改为 TodoId。
- 添加[键]
- 双重检查 DbSet 和 DBContext
但是,正如错误消息本身所指出的那样,EF 清楚地识别了此模型;如果不是,则不会在错误中显示型号名称。
我唯一的疑惑是他的枚举定义或 uint id 是否与它有关?虽然它在课堂之外,所以我不知道如何。谢谢。我正在使用 EF 6.1.3。
【问题讨论】:
-
根据这个问题的答案stackoverflow.com/questions/4918106/… EF 不支持将无符号整数作为主键。
标签: c# entity-framework