Getting Error : System.InvalidOperationException: User-Unhandled
Getting Error : System.InvalidOperationException: User-Unhandled
我正在尝试学习使用 ASP.NET MVC 5 和 Entity Framework 构建一个简单的应用程序。我能够添加迁移并更新数据库。我的应用程序在执行时工作正常。下面是我的应用程序的以下代码。
客户控制器:
using System.Linq;
using System.Web.Mvc;
using WebChatApp.Models;
using System.Data.Entity;
namespace WebChatApp.Controllers
{
public class CustomersController : Controller
{
private ApplicationDbContext _context;
public CustomersController()
{
_context = new ApplicationDbContext();
}
protected override void Dispose(bool disposing)
{
_context.Dispose();
}
// GET: Customers
public ViewResult Index()
{
var customers = _context.Customers.Include(c => c.MembershipType).ToList();
return View(customers);
}
public ActionResult Details(int id)
{
var customer = _context.Customers.SingleOrDefault(c => c.Id == id);
if (customer == null)
return HttpNotFound();
return View(customer);
}
}
}
型号:
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace WebChatApp.Models
{
public class Customer
{
public int Id { get; set; }
[Required]
[StringLength(255)]
public string Name { get; set; }
public bool IsSubscribedToNewsletter { get; set; }
public MembershipType MembershipType { get; set; }
public byte MembershipTypeId { get; set; }
}
}
客户视图:
@model IEnumerable<WebChatApp.Models.Customer>
@{
ViewBag.Title = "Customers";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Customers</h2>
@if (!Model.Any())
{
<p>We don't have any customer yet.</p>
}
else
{
<table class=" table table-bordered table-hover">
<thead>
<tr>
<th>Customer</th>
<th>Membership Type</th>
</tr>
</thead>
<tbody>
@foreach (var customer in Model)
{
<tr>
<td>@Html.ActionLink(customer.Name, "Details","Customers", new { id = customer.Id }, null)</td>
<td>@customer.MembershipType.Name</td>
</tr>
}
</tbody>
</table>
}
迁移:
SQL 服务器中的表:
当我尝试执行执行并从 NavBar 导航到客户模块时,出现以下错误。
System.InvalidOperationException: 'The model backing the
'ApplicationDbContext' context has changed since the database was
created. Consider using Code First Migrations to update the database
(http://go.microsoft.com/fwlink/?LinkId=238269).'
var customers = _context.Customers.Include(c =>
c.MembershipType).ToList();
异常图像:
甚至我通过将目标数据库更新到 DBContext 来检查来自 PackageManagerConsole 的 Entity Framework。即使我已经浏览了 Whosebug 中的一些旧线程。但它对我不起作用。有人可以帮忙吗?
您的数据库模型(出于某种原因)与您的模型不同步。
您可以尝试在程序包管理器控制台中 运行:add-migration test
并查看它是否会产生新的迁移。如果是,您只需 运行 update-database
之后。
如果这没有帮助,请查看此答案:
我正在尝试学习使用 ASP.NET MVC 5 和 Entity Framework 构建一个简单的应用程序。我能够添加迁移并更新数据库。我的应用程序在执行时工作正常。下面是我的应用程序的以下代码。
客户控制器:
using System.Linq;
using System.Web.Mvc;
using WebChatApp.Models;
using System.Data.Entity;
namespace WebChatApp.Controllers
{
public class CustomersController : Controller
{
private ApplicationDbContext _context;
public CustomersController()
{
_context = new ApplicationDbContext();
}
protected override void Dispose(bool disposing)
{
_context.Dispose();
}
// GET: Customers
public ViewResult Index()
{
var customers = _context.Customers.Include(c => c.MembershipType).ToList();
return View(customers);
}
public ActionResult Details(int id)
{
var customer = _context.Customers.SingleOrDefault(c => c.Id == id);
if (customer == null)
return HttpNotFound();
return View(customer);
}
}
}
型号:
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace WebChatApp.Models
{
public class Customer
{
public int Id { get; set; }
[Required]
[StringLength(255)]
public string Name { get; set; }
public bool IsSubscribedToNewsletter { get; set; }
public MembershipType MembershipType { get; set; }
public byte MembershipTypeId { get; set; }
}
}
客户视图:
@model IEnumerable<WebChatApp.Models.Customer>
@{
ViewBag.Title = "Customers";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Customers</h2>
@if (!Model.Any())
{
<p>We don't have any customer yet.</p>
}
else
{
<table class=" table table-bordered table-hover">
<thead>
<tr>
<th>Customer</th>
<th>Membership Type</th>
</tr>
</thead>
<tbody>
@foreach (var customer in Model)
{
<tr>
<td>@Html.ActionLink(customer.Name, "Details","Customers", new { id = customer.Id }, null)</td>
<td>@customer.MembershipType.Name</td>
</tr>
}
</tbody>
</table>
}
迁移:
SQL 服务器中的表:
当我尝试执行执行并从 NavBar 导航到客户模块时,出现以下错误。
System.InvalidOperationException: 'The model backing the 'ApplicationDbContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).'
var customers = _context.Customers.Include(c => c.MembershipType).ToList();
异常图像:
甚至我通过将目标数据库更新到 DBContext 来检查来自 PackageManagerConsole 的 Entity Framework。即使我已经浏览了 Whosebug
您的数据库模型(出于某种原因)与您的模型不同步。
您可以尝试在程序包管理器控制台中 运行:add-migration test
并查看它是否会产生新的迁移。如果是,您只需 运行 update-database
之后。
如果这没有帮助,请查看此答案: