Database.Table对象的错误无效

时间:2013-05-02 08:29:02

标签: c# .net asp.net-mvc-3 visual-studio-2010 sql-server-2005

我正在使用C#和SQL Server 2005开发ASP .Net MVC 3应用程序。

我已经使用他们的控制器(UserController和PosteController)创建了2个模型(User& Poste),这要归功于使用“实体框架”的“读/写操作和视图”创建方法。

当我想访问User或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" />

1 个答案:

答案 0 :(得分:0)

通过在新的查询编辑器窗口中编写此查询来检查数据库是否存在用户表:

select * from dbo.User

可能是你能够运行这个:

select * from User

这是因为此表架构附加了您的默认用户而不是“dbo”对象。

要修复此问题,您需要通过运行以下查询将用户复制到dbo.User:

select * into dbo.User from User