0

I am trying to insert the value into a database using Entity Framework. It is showing an error:

Exception:: An error occurred while updating the entries. See the inner exception for details.

Inner Exception: cannot insert explicit value for identity column in table 'Tbl_Rating' when IDENTITY_INSERT is set to OFF

This is my controller Code

public ActionResult rated(FormCollection form)
{
            int ratedvalue = Convert.ToInt32(form["rating"]);
            using (var db = new Project.Models.EntitiesContext())
            {
                var value = new Project.Models.Tbl_Rating();
                value.Rating = Convert.ToInt32(ratedvalue);
                value.CreatedOn = DateTime.Now;
                value.CreatedBy = 1;
                db.Tbl_Rating.Add(value);
                db.SaveChanges(); // Error Point
            }
            return View(form);
        }
    }

This is my model {

public partial class Tbl_Rating
    {
        [Key]
        public int RateID { get; set; }
        public Nullable<int> Rating { get; set; }
        public string Comment { get; set; }

        [ForeignKey("Tbl_Tasks")]
        public Nullable<int> TaskId { get; set; }


        [ForeignKey("Tbl_Users")]
        public Nullable<int> UserId { get; set; }

        [ForeignKey("Tbl_DailyReport")]
        public Nullable<int> DReportID { get; set; }

        [Required]
        public int CreatedBy { get; set; }

        [Required]
        [DataType(DataType.DateTime)]
        public System.DateTime CreatedOn { get; set; }
        public Nullable<int> ModifiedBy { get; set; }
        public Nullable<System.DateTime> ModifiedOn { get; set; }

        public virtual Tbl_DailyReport Tbl_DailyReport { get; set; }
        public virtual Tbl_Tasks Tbl_Tasks { get; set; }
        public virtual Tbl_Users Tbl_Users { get; set; }
    }

}

Vetri
  • 77
  • 2
  • 3
  • 10
  • Can you add your model? Or table design? – Wahtever Feb 19 '14 at 13:27
  • @Wahtever I have added my model, Pls Check it out – Vetri Feb 19 '14 at 13:30
  • Are you using `RateId` as a `Foreign Key` in another table? if not sure try adding this above it `[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]` – Wahtever Feb 19 '14 at 13:36
  • No, i am not using it as Foreign Key at anywhere. – Vetri Feb 19 '14 at 13:43
  • No doubt, Whatever is right. You must have an identity column which isn't correctly mapped in your model. You can map it with the attirbute, or with f¡the fluent API. – JotaBe Feb 19 '14 at 14:16
  • I can't get you, Please explain clearly about how to resolve this problem. – Vetri Feb 20 '14 at 07:45
  • Hi, idk if you haven't solved this problem Wahtever gave you the proper data annotation to add. However EF won't change the setting in the actual db for Identity insert, so I suggest changing it in SQL management studio or simply removing your old migration and make a fresh one with a new database connection. – Casey Sebben May 23 '14 at 14:32

0 Answers0