0

i want to add a new user into my database but i get this error System.InvalidOperationException: 'The entity type User is not part of the model for the current context.'

UserController.cs Class

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;
 using System.Web.Mvc;
 using Registration.Models;

namespace Registration.Controllers
{
public class UserController : Controller
{
    [HttpGet]
    public ActionResult AddOrEdit(int id=0)
    {
        User userModel = new User();
        return View(userModel);
    }

    [HttpPost]
    public ActionResult AddOrEdit(User userModel)
    {
        using (DBModels db = new DBModels())
        {
            db.Users.Add(userModel);
            db.SaveChanges();
        }
        ModelState.Clear();
        ViewBag.SuccessMessage = "Registration Successful.";
        return View("AddOrEdit", new User());
    }
}
}

User.cs Class inside folder Models

namespace Registration.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

public partial class User
{
    public int UserID { get; set; }
    [Required(ErrorMessage = "This field is required.")]
    public string Username { get; set; }
    [Required(ErrorMessage = "This field is required.")]
    [DataType(DataType.Password)]
    public string Password { get; set; }
    [DataType(DataType.Password)]
    [DisplayName("Confirm Password")]
    [Compare("Password")]
    public string ConfirmPassword { get; set; }
    public bool IsAdmin { get; set; }
}
}
tereško
  • 58,060
  • 25
  • 98
  • 150
Noha Amr
  • 21
  • 1
  • 1
  • 4

0 Answers0