user7629010
user7629010

Reputation:

Invalid object name (ASP.NET MVC)

I have this error

Invalid object name 'dbo.Vacancies'

But I have Model for Vacancies.

Here it is:

public partial class Vacancy
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Vacancy()
    {
        this.Interwiers = new HashSet<Interwier>();
        this.InvitationMails = new HashSet<InvitationMail>();
    }

    [Key]
    public int Vacancy_Id { get; set; }
    [Display(Name="Вакансия")]
    public string VacancyName { get; set; }
    public Nullable<int> CompanyID { get; set; }

    public virtual Company Company { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Interwier> Interwiers { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<InvitationMail> InvitationMails { get; set; }
}

}

Also I have table Vacancy.

This code I have in IdentityModels:

public System.Data.Entity.DbSet<SmartSolutions.Models.Vacancy> Vacancies { get; set; }

Here is code of View where I try to show data from table.

// GET: VacanciesAll
public ActionResult Index()
{
    var vacancies = db.Vacancies.Include(v => v.Company);
    return View(vacancies.ToList());
}

Here is the Table: enter image description here

Here is the table in EF enter image description here Why am I getting an error?

Upvotes: 3

Views: 3899

Answers (3)

live-love
live-love

Reputation: 52494

It could be loooking at the wrong database.

The DbContext class should match the name in the connection string.

Make sure your connection string "name" property is correct.

Example: PortalEntities DbContext should match PortalEntities in connectionStrings.

 public class PortalEntities : DbContext
    {
        public DbSet<Delegate> Delegates { get; set; }
        public DbSet<Status> Statuses { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            Database.SetInitializer<PortalEntities>(null);
            base.OnModelCreating(modelBuilder);
        }
    }

<connectionStrings>

    <add name="PortalEntities" connectionString="Data Source=serverName;Integrated Security=true;Initial Catalog=dbName;" providerName="System.Data.SqlClient"/>

</connectionStrings>

Upvotes: 1

Khaled Harby
Khaled Harby

Reputation: 94

Please check the EF Layers [ SSDL - CSDL - MSL ] this is conflict between your EF layers and database engine

Upvotes: 0

Sadique
Sadique

Reputation: 22841

Check if the Table exists in your Sql Database. Chances are it is not there in your Database, hence, the error.

enter image description here

If the table exists, make sure you are mapping your EF table to the correct table name in DbContext.

Upvotes: 2

Related Questions