2014-10-29 51 views
0

我創建了一個模型類EntityType'Products'沒有定義鍵。定義鍵此的EntityType

namespace EcommerceApplication.Models 
{ 
    public class Products 
    { 
     [Key] 
     [Required] 
     public int ProductID; 
     public string ProductName; 
     public decimal Price; 
     public string ProductImage; 
    } 
} 

我也開始加入Key屬性到我的ID,但其仍表現出

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

這是我的控制器代碼,我正在使用它

public class HomeController : Controller 
    { 
     DbClass _db = new DbClass(); 
     public ActionResult Index() 
     { 
      var products=_db.products.ToList(); 
      return View(products); 
     } 

    } 

我的DbClass如下

public class DbClass:DbContext 
    { 
     public DbSet<Products> products { get; set; } 
    } 

這段代碼有什麼問題。

回答

1

更改您的變量的屬性,例如:

public int ProductID { get; set; } 
+0

哦阿呆夠..非常感謝你。 – 2014-10-29 05:24:39

+0

它一直髮生,歡迎您!將所有公共變量設爲屬性可能是有意義的。 – SBirthare 2014-10-29 05:25:54

+0

我也一樣。 – 2014-10-29 05:27:39

相關問題