0
我使用C#和SQL Server 2005錯誤Database.Table Objet公司是
我創建了2款(用戶&郵政)與他們的控制器開發一個ASP.NET的MVC 3應用程序無效(UserController的& PosteController ),這要歸功於使用「讀/寫操作和視圖,使用實體框架」進行創建的方法。
當我想訪問用戶或郵政的看法,這就是意味着,當我在我的網址來訪問索引把/郵政(或/用戶),在我看來,這個錯誤:
對象名稱'dbo.Poste'無效。或對象名稱'dbo.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" />
沒有,表「用戶」是不存在的,我想,可以通過代碼自動生成。 Infact,我已經創建了表'Poste'並在其中插入一個值。但是,當我添加一個新模型'用戶',我嘗試做與Poste(創建表並插入)相同的東西時,出現此錯誤 – anouar 2013-05-02 08:50:29
您使用EF嗎?請參閱:http://stackoverflow.com/questions/7047900/ef4-code-first-create-new-table – 2013-05-02 08:57:27
是的,我使用EF ,,,我看到你的鏈接,對不起,但我不知道我會在哪裏把這個:Database.SetInitializer(新的DropCreateDatabaseIfModelChanges()); 應用程序啓動?哪裏? –
anouar
2013-05-02 09:03:57