2016-10-27 73 views
0

我已經做了廣泛的研究,對這個錯誤我得到:什麼能導致實體類型除了沒有定義鍵之外沒有鍵定義的錯誤?

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

都在這裏和其他網站:

  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

等...

但是,我沒有像這些問號一樣的問題。他們遇到的問題是他們未能在他們的模型中找出主鍵,我沒有。請看下圖:

型號:

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; } 

     } 
} 

我已經試過(除了閱讀文章):

  1. 更改ID名稱來TodoId。
  2. 添加[關鍵]
  3. 雙檢查DbSet和的DbContext

然而,EF清楚地如由錯誤信息本身指出識別此模型;如果不是,則不會在錯誤中顯示模型名稱。

我唯一的疑問是他可以枚舉定義或有一個uint id必須處理它嗎?儘管如此,我還是沒有看到如何。謝謝。我正在使用EF 6.1.3。

+1

根據對這個問題的答案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

回答

1

實體框架不支持uint類型的屬性(鍵或其他)。試試這個實驗:

將您的Todo.Id屬性更改爲鍵入int(或long)。它會正常工作。

添加類型爲uint的非鍵屬性。它仍然會成功支持你的遷移,但它會完全忽略uint屬性,就像你已經明確排除它一樣。

有建議在Support unisgned integer添加對uint的支持,而EF團隊表示將在EF7中支持。

+0

哇,它絕對應該得到支持,我從來沒有必須使用負ID之前。謝謝! –

相關問題