如何修复“在模型生成期间检测到一个或多个验证错误” - 错误

时间:2014-03-19 14:14:20

标签: c# asp.net asp.net-mvc linq entity-framework

在模型生成期间检测到一个或多个验证错误:

SportsStore.Domain.Concrete.shop_Products :: EntityType' shop_Products'没有定义键。定义此EntityType的密钥。

产品:EntityType:EntitySet'产品'是基于类型' 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; }
}

我做了一些这些,并且效果很好。但这些都是错的。任何人都可以帮助我吗?

5 个答案:

答案 0 :(得分:13)

按惯例,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; }

答案 1 :(得分:8)

将此命名空间引用System.ComponentModel.DataAnnotations添加到您的模型中,然后在您的ID属性上方添加[Key]注释

答案 2 :(得分:2)

namespace:using System.ComponentModel.DataAnnotations.Schema;

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

答案 3 :(得分:1)

您也可以将productID重命名为id

public int id {get; set;}

答案 4 :(得分:0)

这是我在asp.net mvc中针对此类问题的代码

之前

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace glostars.Models
{
    public class WeeklyTagged
    {
        public int TaggedId { get; set; }          
    }
}

之后

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;  //added this line
using System.Linq;
using System.Web;

namespace glostars.Models
{
    public class WeeklyTagged
    {
        [Key]                                   //and this line
        public int TaggedId { get; set; }          
    }
}

现在它可以工作了。谢谢