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; }
}
这段代码有什么问题。
哦阿呆够..非常感谢你。 – 2014-10-29 05:24:39
它一直发生,欢迎您!将所有公共变量设为属性可能是有意义的。 – SBirthare 2014-10-29 05:25:54
我也一样。 – 2014-10-29 05:27:39