Object Name dbo.Poste invalid. or Object Name dbo.User invalid in Entity Framework

Last Reply on May 02, 2013 02:36 AM By Mudassar

Posted on May 02, 2013 02:35 AM

I am developing an ASP .Net MVC 3 application using C# and SQL Server 2005.

I've created 2 models (User & Poste) with their controllers (UserController & PosteController) thanks to the method of creation with 'Read/Write actions and views, using Entity Framework'.

When i would like to access to the view of User or Poste, that's mean when I put /Poste (or /User) in my URL to access to the index, this error appears to me :

 

Object Name 'dbo.Poste' invalid. or Object Name 'dbo.User' invalid.

This is the model of 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; }
    }
}

this is the Controller of User :

 
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);
        }
    }
}

and this the context Class (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>();
        }
    }
}

and this is what I added in my web.config file :


<add name="GammeContext" connectionString="Data Source=SWEET-DE396641E\SQLEXPRESS;database=Flux; Integrated Security=true" providerName="System.Data.SqlClient" />

 

You are viewing reply posted by: Mudassar on May 02, 2013 02:36 AM.
Posted on May 02, 2013 02:36 AM

There is some problem in your Entity Framework. Connection string. Make sure you are connected to right database