0

I've just started my first MVC app following a tutorial. It seems to me that my code matches the tutors exactly however I'm getting an error:

'System.Data.SqlClient.SqlException: Invalid object name 'dbo.Employees'.' '[SqlException (0x80131904): Invalid object name 'dbo.Employees'.]'

My database is called 'Sample', and the web config reflects this.

Can anyone see my obvious mistake?

thanks

Employee Model

namespace MvcApplication2.Models
{
public class Employee
{
    public int EmployeeId { get; set; }
    public string Name { get; set; }
    public string Gender { get; set; }
    public string City { get; set; }    

}
}

Employee Controller

namespace MvcApplication2.Controllers
{
public class EmployeeController : Controller
{


    public ActionResult Details(int id)
    {
        EmployeeContext employeeContext = new EmployeeContext();
        Employee employee = employeeContext.Employees.Single(emp => emp.EmployeeId == id);
        return View(employee);
    }

}
}

EmployeeContext.cs Model

namespace MvcApplication2.Models
{
[Table("tblEmployee")]
public class EmployeeContext : DbContext
{
    public DbSet<Employee> Employees { get; set; }
}
}

WebConfig

<connectionStrings>
<add name="EmployeeContext" connectionString="Data Source={servername};Initial Catalog=Sample;Integrated Security=True"
       providerName="System.Data.SqlClient" />
</connectionStrings>

Global.asax

Database.SetInitializer<MvcApplication2.Models.EmployeeContext>(null);

5 Answers 5

1

You have your data annotation on the wrong class. You have put [Table("tblEmployee")] over the entity, not the context.

It should be above your Employee class like so:

[Table("tblEmployee")]
public class Employee
{
    public int EmployeeId { get; set; }
    public string Name { get; set; }
    public string Gender { get; set; }
    public string City { get; set; }    

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

Comments

1

Hey I am also refering same tutorial series from http://csharp-video-tutorials.blogspot.com/2013/05/part-8-data-access-in-mvc-using-entity.html

Add

using System.ComponentModel.DataAnnotations;

and then use [Key] over the primary key property of your table. i.e EmployeeId.

So, your code will look like:

[Table("tblEmployee")]
public class Employee
{
    [Key]
    public int EmployeeId { get; set; }
    public string Name { get; set; }
    public string Gender { get; set; }
    public string City { get; set; }    

}

Without using key, you will get execption of "One or more validation errors were detected during model generation".

If you need more help, I am glad to help you as I am also learning from same series. :)

Happy learning...!

Comments

0

Found the issue. For some reason it did not use my Data Annotation

[Table("tblEmployee")]

Removing this and renaming my tbl seemed to do the job. Anyone know it ignored my annotation?

1 Comment

You need to put the annotation on the Employee.cs class, not the EmployeeContext class
0

Are you working Code First? You probably need to set the correct initializer so that Entity Framework will create your database if it doesn't exist.

Set this in your Application_Start of your Global.asax:

Database.SetInitializer<EmployeeContext >(new CreateDatabaseIfNotExists<EmployeeContext >());

1 Comment

Hi. No I've already created the sql server db. My Global.asax file contains: 'Database.SetInitializer<MvcApplication2.Models.EmployeeContext>(null);'
0

1.This error is Very Simple I think your connectionn Strinng is not working so, 1. Right click on Server Explorer in mvc 2. Go on "Data Connectionn" and right click on "Data Connectionn" and then click on "Add Connection" Click on "Microsoft Sql Server" then "ok" write the server name for Exapmle my Server Name is in which database is created "(localdb)\ProjectsV13" 3. Select or enter a database name and test connection and test connection succeded 4. click on advance select and copy the for example "Data Source=(localdb)\ProjectsV13;Integrated Security=True" and then go on your project "web.config" file and add Connection String Here Maybe youre 1.EmployeeContext 2.connectionString="Data Source=(localdb)\ProjectsV13;Initial Catalog=Database2;Integrated Security=True" are different other code remain same. Thankyou.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.