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


1 answer


The object type user is not part of the model for the current context.



This is the important part. Is the User object part of your MyDatabase2Entities context? This is not true.

+4


source







All Articles