我在代码中收到以下异常。在EntityFramework.dll中发生类型'System.InvalidOperationException'的异常,但在注册时未在用户代码中处理
在EntityFramework.dll中发生了“System.InvalidOperationException”类型的异常,但未在用户代码中处理。
附加信息:实体类型用户不是当前上下文的模型的一部分。
以下是我的注册控制器。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Registration2.Controllers
{
public class userController : Controller
{
// GET: user
public ActionResult Index() => View();
public ActionResult Register() => View();
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Register(Registration2.user U)
{
if (ModelState.IsValid)
{
using (MyDatabase2Entities dc = new MyDatabase2Entities())
{
//you should check duplicate registration here
// dc.users.Add
dc.users.Add(U);
dc.SaveChanges();
ModelState.Clear();
U = null;
ViewBag.Message = "Successfully Registration Done";
}
}
return View(U);
}
}
}
我完全陌生的ASP.net和MVC所以请告诉我是否错过了什么,我将它张贴在这里。
如果你右键点击你的数据模型,并选择“验证”,你会得到任何错误? –
将代码包装在try-catch块中,向我们展示异常消息和堆栈跟踪,我们将能够为您提供更多帮助。 –
此外,为什么你将用户设置为空,当它保存后返回到视图? –