本文介绍了实体类型没有键定义 - 代码优先的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很喜欢MVC以及实体框架。我搜索很多,并且找到几个类似的问题(例如或使用流畅的语法,使用


I'm new to MVC as well as the entity framework. I searched lot and find few similar questions (e.g. Entity Type Has No Key Defined) but they don't solve my problem.

namespace MvcAppInvoice.Models
{
    public class Customer
    {
        public int CustomerID { get; set; }
        public string FirstName { get; set; }
        public string SurName { get; set; }
        public virtual CustomerType Type { get; set; }
    }

    public class CustomerType
    {
        public int TypeId { get; set; }
        public string TypeName { get; set; }
        public virtual ICollection<Customer> customers { get; set; }
    }
}

When I try to add the controller it gives the following error:

解决方案

Typically, code first by convention implicitely sets key for entity type if property is named Id or TypeName+Id. In your case, TypeId is neither of them, so you should explicitely mark it as key, using KeyAttribute or with fluent syntax, using EntityTypeConfiguration.HasKey Method

这篇关于实体类型没有键定义 - 代码优先的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 00:43