Reputation:
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 in EF
Why am I getting an error?
Upvotes: 3
Views: 3899
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
Reputation: 94
Please check the EF Layers [ SSDL - CSDL - MSL ] this is conflict between your EF layers and database engine
Upvotes: 0
Reputation: 22841
Check if the Table exists in your Sql Database. Chances are it is not there in your Database, hence, the error.
If the table exists, make sure you are mapping your EF table to the correct table name in DbContext.
Upvotes: 2