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; }
}
}