2017-06-25 46 views
0

私はAsp.netを新規に登録しています。私はこのエラーが発生しています。System.InvalidOperationException:エンティティタイプUserが現在のコンテキストのモデルの一部ではありません

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

答えて

0

あなたは方法このようなOnModelCreatingUserエンティティに関するDbContextを伝える必要があります。

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

か、[EntityTypeConfiguration<T>][1]クラスを使用してmodelBuilder.Configurationsコレクションに継承された子クラスのインスタンスを追加することができます。

これを行うと問題が解決するはずです。

関連する問題