簡體   English   中英

System.InvalidOperationException:實體類型User不是當前上下文的模型的一部分

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

我是新的Asp.net,在進行注冊時。 我收到此錯誤。

System.InvalidOperationException:實體類型User不是當前上下文的模型的一部分。

怎么解決這個?

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")
}

您需要告訴DbContext有關此方法OnModelCreating內的User實體。

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

或者您可以使用[EntityTypeConfiguration<T>][1]類並將繼承的子類的實例添加到modelBuilder.Configurations集合中。

這樣做可以解決問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM