2014-03-19 1 views
2

하나 이상의 유효성 검사 오류 "하나 이상의 유효성 검사 오류가 모델 생성 중에 발견되었습니다"해결 방법 :모델 생성 중에 감지 된 -error

SportsStore.Domain.Concrete.shop_Products : EntityType 'shop_Products은'있다 정의 된 키가 없습니다. 이 EntityType의 키를 정의하십시오.

제품 : EntityType : EntitySet 'Products'는 정의 된 키가없는 'shop_Products'유형을 기반으로합니다.

public ViewResult Index() 
    { 

     ProductsListViewModel viewModel = new ProductsListViewModel 
     { 
      Products = repository.Products 
       .Where(p => p.CategoryId == 100) 
       .OrderByDescending(p=>p.ProductID) 
       .Take(5) 
     }; 
     return View(viewModel); 
    } 


@foreach (var p in Model.Products) 
{ 

<a href="">@p.ProductName</a> 
} 


public class shop_Products { 
    public int ProductID { get; set; } 
    public string ProductName { get; set; } 
    public int CategoryId { get; set; } 
    public Nullable<int> CategoryPropertyId { get; set; } 
    public string PropertyValue { get; set; } 
    public Nullable<int> ProductBrandId { get; set; } 
    public Nullable<decimal> MarketPrice { get; set; } 
    public decimal Price { get; set; } 
    public Nullable<decimal> UserPrice { get; set; } 
    public string TitleKeyword { get; set; } 
    public string MetaKeyword { get; set; } 
    public string MetaDescription { get; set; } 
    public string PhotoName { get; set; } 
    public string PhotoPath { get; set; } 
    public string smallPhotos { get; set; } 
    public string BigPhotos { get; set; } 
    public string URL { get; set; } 
    public double Discount { get; set; } 
    public int Inventory { get; set; } 
    public string ShortDesc { get; set; } 
    public bool IsAccessories { get; set; } 
    public bool IsGroup { get; set; } 
    public bool IsTopService { get; set; } 
    public string Accessorices { get; set; } 
    public string PeopleGroup { get; set; } 
    public string TopService { get; set; } 
    public string Contents { get; set; } 
    public string Parameter { get; set; } 
    public string PackingList { get; set; } 
    public string Service { get; set; } 
    public string Professional { get; set; } 
    public bool IsParameter { get; set; } 
    public bool IsPackingList { get; set; } 
    public bool IsService { get; set; } 
    public bool IsProfessional { get; set; } 
    public Nullable<bool> IsEnable { get; set; } 
    public Nullable<bool> IsCommend { get; set; } 
    public Nullable<bool> IsTop { get; set; } 
    public Nullable<bool> IsBest { get; set; } 
    public string ProductBrandType { get; set; } 
    public string Manufacturer { get; set; } 
    public string Makein { get; set; } 
    public string weight { get; set; } 
    public System.DateTime InputTime { get; set; } 
    public Nullable<int> Sort { get; set; } 
    public Nullable<int> SeeCount { get; set; } 
} 

나는 이들 중 몇 가지를 만들고, 이러한 늘지 작동 wrong.Can 아무도 나를 도와?

답변

12

약식으로 EF는 Id 또는 [type name]Id 필드를 기본 키로 사용합니다. 여기를 참조하십시오 : http://msdn.microsoft.com/en-us/library/system.data.entity.modelconfiguration.conventions.idkeydiscoveryconvention%28v=vs.103%29.aspx

shop_Products이라는 이름이 있지만 키를 ProductID (가정)으로 설정하면 규칙에 따라 찾을 수 없습니다. 그래서 당신은 필드 이름을 변경하거나 이름을 입력하거나 추가 데이터 주석 [Key]ProductID 이상과 같이 할 수 있습니다 :

[Key] 
public int ProductID { get; set; } 
6

모델에이 네임 스페이스 심판 System.ComponentModel.DataAnnotations을 추가 한 다음 ID 속성

1
을 위 [Key] 주석을 추가

productID를 id로 바꿀 수도 있습니다

public int id {get; set;} 
관련 문제