简体   繁体   中英

System.InvalidOperationException: The entity type User is not part of the model for the current context

I'm new Asp.net and while making registration. I'm getting this Error.

System.InvalidOperationException: The entity type User is not part of the model for the current context.

How to resolve this?

UserController

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient; 
using System.Web.Mvc;
using loginProject.Models;
namespace loginProject.Controllers
{
public class UserController : Controller
{
    // GET: User
    public ActionResult Index()
    {
        return Content("Successfully");
    }
    public ActionResult Register(int id = 0)
    {
        User userModel = new User();
        return View(userModel);
    }

    [HttpPost]
    public ActionResult Register(User account)
    {
        using (DbModels db = new DbModels())
        {
            db.Users.Add(account);
            db.SaveChanges();
        }
        ModelState.Clear();
        ViewBag.Message = account.FirstName + "  " + account.LastName + " Successfully Registered.";
        return View("Register", new User());
    }

}
}

User.cs

namespace loginProject.Models
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Data.Entity;

public partial class User : DbModels
{
    public int UserID { get; set; }
    [Required(ErrorMessage = "First Name is Required")]
    public string FirstName { get; set; }

    [Required(ErrorMessage = "Last Name is Required")]
    public string LastName { get; set; }

    [Required(ErrorMessage = "Email is Required")]
    public string Email { get; set; }

    [Required(ErrorMessage = "User Name is Required")]
    public string UserName { get; set; }

    [Required(ErrorMessage = "Password is Required")]
    [DataType(DataType.Password)]
    public string Password { get; set; }

    [Compare("Password", ErrorMessage = "Please Confirm Password First")]
    [DataType(DataType.Password)]
    public string ConfirmPassword { get; set; }



   }
}

DbModel.Context.cs

namespace loginProject.Models
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;

public partial class DbModels : DbContext
{
    public DbModels()
        : base("name=DbModels")
    {
    }    
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }

    public virtual DbSet<User> Users { get; set; }

    }
}

Register.cshtml

@model loginProject.Models.User

@{
    ViewBag.Title = "User Registeration";
 }

 <h2>Register</h2>


@using (Html.BeginForm("Register", "User", FormMethod.Post))
{
    @Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>User</h4>
    <hr />
    <div class="form-group">
        <div class="col-md-8">
            <label class="label-success">
                @ViewBag.SuccessMessage
            </label>
        </div>
    </div>
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.FirstName, htmlAttributes: new { 
        @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.FirstName, new { htmlAttributes = 
            new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.FirstName, "", new { 
            @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.LastName, htmlAttributes: new { @class 
    = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.LastName, new { htmlAttributes = 
    new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.LastName, "", new { 
   @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = 
   "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Email, new { htmlAttributes = new 
   { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Email, "", new { 
   @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.UserName, htmlAttributes: new { @class 
   = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.UserName, new { htmlAttributes = 
  new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.UserName, "", new { 
  @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Password, htmlAttributes: new { @class 
 = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Password, new { htmlAttributes = 
 new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Password, "", new { 
 @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.ConfirmPassword, htmlAttributes: new { 
      @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.ConfirmPassword, new { 
  htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.ConfirmPassword, "", n new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
 </div>
}

<div>
     @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
     @Scripts.Render("~/bundles/jqueryval")
}

You need to tell the DbContext about the User entity inside the method OnModelCreating like this.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<User>().ToTable("users");
}

or you can use [EntityTypeConfiguration<T>][1] class and add instance of inherited child class to the modelBuilder.Configurations collection.

Doing this should resolve the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM