EF 5 - The number of primary keys transmitted must match the number of primary key values ​​defined on the site

I came across this error message when using EF5. Think about whether anyone has an answer to this question.

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; }
    }
}
+5
source share
1 answer

You have not declared a primary key for the object. You do this by marking the primary key column with the [Key} attribute. Assuming this is an Id property, the code would look like this:

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

All Articles