2012-06-20 30 views
3

之前沒有看到這個錯誤,粗略的網頁搜索變得很少。這裏是(我認爲)有問題的代碼:實體框架代碼優先:「在模型中找不到指定的表」foo「。」

this.HasMany(a => a.ListItems).WithRequired() 
    .Map(m => 
     { 
      m.MapKey("AttributeId"); 
      m.ToTable("ProductAttributeListItem"); 
     } 
    ) 
; 

下面是完整的錯誤:

The specified table 'ProductAttributeListItem' was not found in the model. Ensure that the table name has been correctly specified.

表是存在的,拼寫正確。

缺乏搜索結果讓我覺得我失去了一些明顯的東西。那可能是什麼?

回答

4

如果要定義實體的表名ListItems是指的你需要做的是在實體,而不是在映射關係:

modelBuilder.Entity<ListItem>() // or whatever the entity is called 
    .ToTable("ProductAttributeListItem"); 

而且從Map行動刪除m.ToTable

+0

這工作。奇怪的是,我以前使用SqlExpress並指定了'.ToTable(「foo」)。MapKey(「bar」)'進行映射,並且工作正常。轉移到Sql Enterprise改變了一些事情,它開始抱怨'表未找到'。 – TheVillageIdiot