An exception of type "System.InvalidOperationException" occurred in EntityFramework.dll but was not handled in user code during registration
I am getting the following exception in my code.
An exception of type "System.InvalidOperationException" occurred in EntityFramework.dll but was not handled in user code.
Additional information: The user of the object type is not part of the model for the current context.
Below is my registration controller.
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);
}
}
}
I'm completely new to ASP.net and MVC, so please let me know if I missed anything, I'll post it here.
+3
source to share