我是新的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")
}
答案 0 :(得分:0)
您需要告诉DbContext
这个方法User
内的OnModelCreating
实体。
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<User>().ToTable("users");
}
或者您可以使用[EntityTypeConfiguration<T>][1]
类并将继承的子类的实例添加到modelBuilder.Configurations
集合。
这样做可以解决问题。