I am new to Visual Studio MVC3 and trying to connect to a database. I have my connection string in the web.config file:
add name=”con” connectionString=”Data Source=190.190.200.100,1433;Network Library=DBMSSOCN;Initial Catalog=myDataBase;
User ID=myUsername;Password=myPassword;” providerName=”System.Data.SqlClient”
However, the server has multiple tables. How/where will I specify which table to use when querying the database?
EDIT:
For example, I am looking at this example. How does the application differentiate between the tables to display data? When you call return View(db.Students.ToList()) as in the example in the link, how does the application know to look in the student table and not in the enrollment table?
The
db.Studentspart comes from Entity Framework.Read the “Creating the Database Context” section in the link that you posted.
You will find the following code there:
This sets up the database context, which is basically Entity Framework’s “setup”, from where it knows which C# class it has to map to database tables.
So
db.Students(from your question) is actually aDbSet<Student>.Entity Framework’s default convention looks like this: it tries to map a class to a table with the same name.
Usually, it would map the
Studentclass to a table namedStudents(pluralized), but you can change/override these conventions…which they also did in this example, in this line:This is also explained in the tutorial, directly under the above code.
Quote from the tutorial: