I want to turn on IDENTITY_INSERT before inserting data into the database. But when I run my code it throws an error
"Cannot find the object "UniversityDB.StudentRegistrationModels" because it does not exist or you do not have permissions"
My control code is:
public ActionResult Create([Bind(Include = "StudentId,CourseId,IsPaymentComplete")] StudentRegistrationModels studentRegistration)
{
using (var db = new UniversityDBContext())
{
db.StudentRegistration.Add(studentRegistration);
db.Database.ExecuteSqlCommand("SET IDENTITY_INSERT UniversityDB.StudentRegistrationModels ON;");
db.SaveChanges();
db.Database.ExecuteSqlCommand("SET IDENTITY_INSERT UniversityDB.StudentRegistrationModels OFF");
}
}
and database model is:
public class StudentRegistrationModels
{
[Key, ForeignKey("Student"), Required]
public int StudentId { get; set; }
[ForeignKey("Course"), Required]
public int CourseId { get; set; }
public DateTime EnrollDate { get; set; }
public bool IsPaymentComplete { get; set; }
public virtual StudentModel Student { get; set; }
public virtual CourseModels Course { get; set; }
}
Please help me to fix it