Working with Models and connection string in MVC5 Using Visual studio 2013 RC
Introduction
This article explains about how to work with model in MVC,Connection string and Entity Framework ...and how to manage database through a class and to create or maintain the Connection strings.
Let’s look into the sample Implementation:-
Step 1:-Create a Sample Asp.net MVC Application ..Here Iam naming my Solution as MVC Students
Step 2:-Now add a Model class by Right Clicking on the Models Folder as shown below and Provide a specific name
Step 3:-Now Let's add some properties to the class as shown below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MVCStudents.Models
{
public class Students
{
public int studentID { get; set; }
public string studentName { get; set; }
public int studentmarks { get; set; }
public int Test { get; set; }
}
}
The Students class represents Students in the database. In the preceding example, each instance of the Student object represents a row element within a database table and each property of this Student class will map to a column in the table.
Add Ado.net Entity Model which will Create a DBcontext
Now, after adding the Model class add the StudentsDBContext class in the same students class file as shown below.
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace MVCStudents.Models
{
public class Students
{
public int studentID { get; set; }
public string studentName { get; set; }
public int studentmarks { get; set; }
public int Test { get; set; }
}
public class StudentsDBcontext:DbContext
{
public DbSet Students { get; set; }
}
}
The class named StudentsDBcontext class related to the Entity Framework Students database context. Which is helpful for retrieving, storing and updating the Students class instances in the database. The DB Context class that is provided by the Entity Framework is the base class of StudentsDBcontext.
Creating a Connection String
In the application, the StudentsDBContext class is responsible for handling the task of connecting to database and to map the Students objects to database records. The Entity Framework automatically connects the database and uses it. Now, let us know how to add a connection string explicitly in the Web.Configfile
Now obtain a Database connection string in easy way
- Open a Notepad
- Click on Save As.
- Write filename as something.udl, file type All files, click Save.
- Now run something.udl.
- Click on Provider tab. Select the provider and click next.
- Give the Database Path, Give Username, and Password, if any.
- Click on Test Connection.
If it is succeeded open the file with notepad and copy the connection string and place it in webconfig as shown below…
<connectionStrings>
<add name="ConnStringDb1" connectionString="Data Source=RAMASAGAR-PC\SQLEXPRESS;User ID=sa;Password=pass;Initial Catalog=Students;Integrated Security=True;"providerName="System.Data.SqlClient/>
</connectionStrings>
Conclusion
This article explains you the quick approach to how to add a model Class in your MVC Application and how to add a connection string…….You can see my further articles on MVC5 Using Visual studio 2013 RC