我到处寻找,我无法弄清楚我的asp项目存在什么问题。
基本上我收到此错误
无效的对象名称dbo.Enquires。
它正在寻找一张不存在的桌子,我从不在任何地方提过这张桌子,不知道为什么要找它。
EnquireController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Data.Entity;
using System.Net;
using PetePersonalTrainerFinal.Models;
namespace PetePersonalTrainerFinal.Controllers
{
public class EnquireController : Controller
{
private ApplicationDbContext db = new ApplicationDbContext();
[Authorize(Roles ="Admin, Mod")]
// Enquire
public ActionResult Index(string SearchName)
{
var items = from i in db.Enquiries select i;
if (!String.IsNullOrEmpty(SearchName))
{
items = items.Where(i => i.Name.Contains(SearchName));
}
return View(items);
}
[Authorize(Roles ="Admin, Mod")]
// Details
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Enquire enquire = db.Enquiries.Find(id);
if (enquire == null)
{
return HttpNotFound();
}
return View(enquire);
}
// Create
public ActionResult Create()
{
return View();
}
// Post Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create ([Bind(Include ="Id,Name,Address,Suburb,State,Postcode,Email,Phone,Description")] Enquire enquire)
{
if (ModelState.IsValid)
{
db.Enquiries.Add(enquire);
db.SaveChanges();
TempData["message"] = "Your enquiry has been successufully submitted.";
return RedirectToAction("Index", "Home");
}
return View(enquire);
}
// Edit
[Authorize(Roles = "Admin")]
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Enquire enquire = db.Enquiries.Find(id);
if (enquire == null)
{
return HttpNotFound();
}
return View(enquire);
}
//Post edit
[HttpPost]
[ValidateAntiForgeryToken]
[Authorize(Roles = "Admin")]
public ActionResult Edit([Bind(Include = "Id,Name,Address,Suburb,State,Postcode,Email,Phone,Description")] Enquire enquire)
{
if (ModelState.IsValid)
{
db.Enquiries.Add(enquire);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(enquire);
}
// Delete
[Authorize(Roles = "Admin")]
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Enquire enquire = db.Enquiries.Find(id);
if (enquire == null)
{
return HttpNotFound();
}
return View(enquire);
}
// Post Delete
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
[Authorize(Roles = "Admin")]
public ActionResult DeleteConfirmed(int id)
{
Enquire enquire = db.Enquiries.Find(id);
db.Enquiries.Remove(enquire);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}
Enquire.cs模型
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace PetePersonalTrainerFinal.Models
{
public class Enquire
{
public int Id { get; set; }
[Required(ErrorMessage = "Name is required")]
[Display(Name = "Name")]
public string Name { get; set; }
[Required(ErrorMessage = "Address is required")]
[Display(Name = "Address")]
public string Address { get; set; }
[Required(ErrorMessage = "Suburb is required")]
[Display(Name = "Suburb")]
public string Suburb { get; set; }
[Required(ErrorMessage = "State is required")]
[Display(Name = "State")]
public string State { get; set; }
[Required(ErrorMessage = "Postcode is required")]
[Display(Name = "Postcode")]
public string Postcode { get; set; }
[Required(ErrorMessage = "Email is required")]
[Display(Name = "Email")]
public string Email { get; set; }
[Required(ErrorMessage = "Phone is required")]
[Display(Name = "Phone")]
public string Phone { get; set; }
[Required(ErrorMessage = "Description is required")]
[Display(Name = "Description")]
public string Description { get; set; }
}
}
那就是The Page我得到了错误。
@model IEnumerable<PetePersonalTrainerFinal.Models.Enquire>
@{
ViewBag.Title = "Index";
}
<div class="container">
<h2>Index</h2>
@if (User.IsInRole("Admin"))
{
<p>
@Html.ActionLink("Create New", "Create")
</p>
}
@if (User.IsInRole("Admin") || User.IsInRole("Mod"))
{
using (Html.BeginForm())
{
<p>
Name: @Html.TextBox("SearchName")
<input type="submit" value="Search" />
</p>
}
}
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Address)
</th>
<th>
@Html.DisplayNameFor(model => model.Suburb)
</th>
<th>
@Html.DisplayNameFor(model => model.State)
</th>
<th>
@Html.DisplayNameFor(model => model.Postcode)
</th>
<th>
@Html.DisplayNameFor(model => model.Email)
</th>
<th>
@Html.DisplayNameFor(model => model.Phone)
</th>
<th>
@Html.DisplayNameFor(model => model.Description)
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Address)
</td>
<td>
@Html.DisplayFor(modelItem => item.Suburb)
</td>
<td>
@Html.DisplayFor(modelItem => item.State)
</td>
<td>
@Html.DisplayFor(modelItem => item.Postcode)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
<td>
@Html.DisplayFor(modelItem => item.Phone)
</td>
<td>
@Html.DisplayFor(modelItem => item.Description)
</td>
<td>
@Html.ActionLink("Update", "Edit", new { id = item.Id}) |
@Html.ActionLink("Details", "Details", new { id = item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id = item.Id })
</td>
</tr>
}
</table>
由于某些原因,我一直收到错误,我似乎无法修复它。我对asp.net没有多少经验。这是我得到的错误http://pastebin.com/Gmmhn0VX
它一直在寻找dbo.Enquires并且不存在,我也搜索了整个项目,我看不到任何东西指的是dbo.Enquires。
任何帮助都将不胜感激。
编辑:这是ApplicationDBContext
using System.Data.Entity;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
namespace PetePersonalTrainerFinal.Models
{
// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/? LinkID=317594 to learn more.
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
public System.Data.Entity.DbSet<PetePersonalTrainerFinal.Models.Price> Prices { get; set; }
public System.Data.Entity.DbSet<PetePersonalTrainerFinal.Models.Enquire> Enquiries { get; set; }
}
}