我正在使用 C# 和 SQL Server 2005 开发一个 ASP .Net MVC 3 应用程序。
由于使用“使用实体框架读取/写入操作和视图”的创建方法,我已经使用它们的控制器(UserController 和 PosteController)创建了 2 个模型(用户和 Poste)。
当我想访问用户或 Poste 的视图时,这意味着当我将 /Poste(或 /User)放入我的 URL 以访问索引时,我会出现此错误:
对象名称“dbo.Poste”无效。或对象名称“dbo.User”无效。
这是 User 的模型:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Web.Mvc;
using System.Web.Security;
using System.Data.SqlClient;
using System.Data.Entity;
namespace MvcApplication2.Models
{
public class User
{
[Required]
[Key]
[Display(Name = "Matricule :")]
public string Matricule { get; set; }
[Required]
[Display(Name = "Nom :")]
public string Nom_User { get; set; }
[Required]
[StringLength(100, ErrorMessage = "Le {0} doit avoir au minimum {2} caractères.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Mot de passe :")]
public string passWord { get; set; }
[Required]
[Display(Name = "Type :")]
public string Type_User { get; set; }
[Required]
[Display(Name = "ID_UF :")]
public string ID_UF { get; set; }
}
}
这是用户的控制器:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication2.Models;
namespace MvcApplication2.Controllers
{
public class UserController : Controller
{
private GammeContext db = new GammeContext();
//
// GET: /User/
public ViewResult Index()
{
return View(db.Users.ToList());
}
//
// GET: /User/Details/5
public ViewResult Details(string id)
{
User user = db.Users.Find(id);
return View(user);
}
//
// GET: /User/Create
public ActionResult Create()
{
return View();
}
//
// POST: /User/Create
[HttpPost]
public ActionResult Create(User user)
{
if (ModelState.IsValid)
{
db.Users.Add(user);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(user);
}
//
// GET: /User/Edit/5
public ActionResult Edit(string id)
{
User user = db.Users.Find(id);
return View(user);
}
//
// POST: /User/Edit/5
[HttpPost]
public ActionResult Edit(User user)
{
if (ModelState.IsValid)
{
db.Entry(user).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(user);
}
//
// GET: /User/Delete/5
public ActionResult Delete(string id)
{
User user = db.Users.Find(id);
return View(user);
}
//
// POST: /User/Delete/5
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(string id)
{
User user = db.Users.Find(id);
db.Users.Remove(user);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
}
这是上下文类(GammeContext):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace MvcApplication2.Models
{
public class GammeContext:DbContext
{
public DbSet<Poste> Postes { get; set; }
public DbSet<User> Users { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<System.Data.Entity.ModelConfiguration.Conventions.PluralizingTableNameConvention>();
}
}
}
这就是我在 web.config 文件中添加的内容:
<add name="GammeContext" connectionString="Data Source=SWEET-DE396641E\SQLEXPRESS;database=Flux; Integrated Security=true" providerName="System.Data.SqlClient" />