3

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?

0

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

@E.S your table name is Vacancy or Vacancies. I think that is the problem. Your mapping is not correct in EF.
Vacancy , but I have this ` public System.Data.Entity.DbSet<SmartSolutions.Models.Vacancy> Vacancies { get; set; }` so it must be all okay
Change it to System.Data.Entity.DbSet<SmartSolutions.Models.Vacancy> Vacancy { get; set; } and try. It needs to map the name of Object not the type.
1

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>

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.