我想保存對數據庫使用EntityFramework的更改,但是我得到一個null excpetion錯誤,但我知道數據不爲空。ArgumentNullException當保存到數據庫
public partial class Model1
{
public int ID { get; set; }
public string Code { get; set; }
public bool Compulsary { get; set; }
public string Year { get; set; }
}
private Entity db = new Entity();
[HttpPost]
public ActionResult Add(string code)
{
HttpCookie cookie = Request.Cookies["Login"];
int ID = int.Parse(cookie["ID"]);
Model1 model1 = new Model1
{
ID = ID,
Code = code,
Compulsary = false,
Year = "P2"
};
db.Model1.Add(model1);
db.SaveChanges();
return View();
}
當它到達db.SaveChanged然後我得到一個錯誤。
剛剛添加到代碼中。 – user1938460
在這種情況下來自實體框架的'ArgumentNullException'通常意味着數據庫中需要的字段在您的模型中爲null。異常是否有InnerException? – CodeCaster
@CodeCaster你是對的。 –