EF 5 - 传递的主键值的数量必须与实体上定义的主键值的数量相匹配

时间:2012-07-13 02:24:59

标签: c# entity-framework entity-framework-4

使用EF5时遇到此错误消息。想知道是否有人有这个答案。

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Budget.Data
{

    [Table("BudgetItems")]
   public class BudgetItemRepository
    {
         [Column("MdaCode")]
        public int MDACode { get; set; }
        [Column("PersonalCost")]
        public double PersonnelCost { get; set; }
        [Column("OverheadCost")]
        public double OverheadCost { get; set; }
        [Column("RecurrentCost")]
        public int RecurrentCost { get; set; }
        [Column("CapitalCost")]
        public double Capital { get; set; }

        public double Allocation { get; set; }
        [Column("BudgetYear")]
        public String BudgetYear { get; set; }
         [Column("RecordCreatedDate")]
        public DateTime DateCreated { get; set; }
        [Column("RecordLastModifiedDate")]
        public DateTime LastModifiedDate { get; set; }

        public virtual IList<BudgetItemRepository> budgetitems { get; set; }

        public virtual IList<BudgetLineItemRepository> budgetlineitems { get; set; }
         [Column("BudgetItemID")]
        public int Id { get; set; }
    }
}

1 个答案:

答案 0 :(得分:3)

您没有为实体声明主键。您可以通过使用[Key}属性标记主键列来完成此操作。假设这是Id属性,代码如下:

[Column("BudgetItemID")]
[Key]
public int Id { get; set; }