由于某种原因,我在db.SaveChanges()时收到以下错误;指令:
Cannot insert the value NULL into column 'UserId',table 'XXXXXXXXX_Dev.dbo.Portfolios'; column does not allow nulls. INSERT fails. The statement has been terminated.
控制器代码:
[HttpPost] [Authorize] public ActionResult Create(Portfolio portfolio) { if (ModelState.IsValid) { portfolio.UserId = (Guid)Membership.GetUser().ProviderUserKey; db.AddToPortfolios(portfolio); db.SaveChanges(); } return View("MyPortfolios"); }
我已经通过调试器并确认正在填充UserID.
更新:
我试过改变db.AddToPortfolios(组合);到db.Portfolios.AddObject(portfolio);但它仍然有同样的问题.
Portfolios是一个ObjectSet,我应该使用Attach()方法吗?