I am currently using ASP.NET MVC4 and trying to add a new entry to a table which has an Integer
as primary key. I know about the identity issue and set it to true, I also know about GUIDs but they are used for unique identifiers and not integers.
Here is where I add a new row knowing that my Primary key is a column named SheetId
if (ModelState.IsValid)
{
var sheet = db.Sheets.Create();
sheet.Email = Request.Cookies["UserInfo"]["Email"];
sheet.ApprovedDays = "0000000";
sheet.DateStarted = DateTime.Now;
db.Sheets.Add(sheet);
db.SaveChanges();
return RedirectToAction("Index", "Home");
}
And this is the Sheet Model
public class Sheet
{
[Key]
[Required]
public int SheetId { get; set; }
[Required]
public DateTime StartDate { get; set; }
[Required]
public string Email { get; set; }
public string ApprovedDays { get; set; }
//
public virtual UserModel UserModel { get; set; }
public virtual ICollection<Task> Task { get; set; }
}
And this is the exception I get
Exception Details: System.Data.SqlServerCe.SqlCeException: The column cannot be modified. [ Column name = SheetId ]
I've solved the Issue by only deleting my .EDMX Context file and Recreated it again and voilà it worked !! Thanks also for your efforts guys