I am getting this error when I try to save to my database using EF.
Cannot insert the value NULL into column 'CompanyId' column does not allow nulls.
From the message it is quite obvious what the problem is, but it don't see any reasons in my code to have this problem.
Here is my model :
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int CompanyId { get; set; }
public string CompanyName { get; set; }
public string CompanyCountry { get; set; }
public string CompanyCity { get; set; }
public string CompanyPostalCode { get; set; }
public string CompanyPhoneNumber { get; set; }
public string EmailCA { get; set; }
Controller :
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "CompanyId,CompanyName,CompanyAddress,CompanyCountry,CompanyCity,CompanyPostalCode,CompanyPhoneNumber,EmailCA")] Company company)
{
if (ModelState.IsValid)
{
db.Companies.Add(company);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(company);
}
What I am expecting from these attributes is to have an auto generated key from them, but obviously this doesn't work.
Can someone help and explain, please?