我已經做了廣泛的研究,對這個錯誤我得到:什麼能導致實體類型除了沒有定義鍵之外沒有鍵定義的錯誤?
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; }
}
}
的DbContext:
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。
根據對這個問題的答案http://stackoverflow.com/questions/4918106/is-it-good-idea-to-use-uint-instead-of-int-as-the-primary-key- in-data-model-clas EF不支持將未簽名的整數作爲主鍵。 – user2697817