简体   繁体   English

脚手架控制器 mvc5 时出错

[英]error when scafolding controller mvc5

Hi when i try adding a controller using the controller actions and entity framework, it gives me an error saying:嗨,当我尝试使用控制器操作和实体框架添加控制器时,它给了我一个错误消息:

"there was an error with the selected code generator: unable to retrieve metadata for 'Music.Model.Customer'. one or more validation errors were detected during model generation: “所选代码生成器出错:无法检索‘Music.Model.Customer’的元数据。在模型生成过程中检测到一个或多个验证错误:

Music.Data.Customer : EntityType 'Customer' has no key defined. Music.Data.Customer : EntityType 'Customer' 没有定义键。 Define the key for this entity type.定义此实体类型的键。

Images: EntityType:EntitySet 'images' is based on type 'Customer that has no keys defined.图像:EntityType:EntitySet 'images' 基于未定义键的类型 'Customer'。

I did build my context before adding the controller在添加控制器之前,我确实构建了我的上下文

In your model, you should decorate a property with the [Key] attribute.在您的模型中,您应该使用[Key]属性装饰一个属性。 For example:例如:

public partial class Customer
{
    [Key]
    public int CustomerId { get; set; }
    ...
    ...

}

Entity Framework will recognise the primary key of a table if the column is named Id.如果列名为 Id,实体框架将识别表的主键。 If your table does not have a primary key field named Id, you can identify the primary key field by setting the [Key] attribute.如果您的表没有名为 Id 的主键字段,您可以通过设置[Key]属性来标识主键字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM