Working with model class and connection string in MVC 5 using Visual Studio 2013 RC

Rama Sagar
Posted by in ASP.NET MVC category on for Beginner level | Points: 250 | Views : 5586 red flag
Rating: 3 out of 5  
 1 vote(s)

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 


Page copy protected against web site content infringement by Copyscape

About the Author

Rama Sagar
Full Name: RamaSagar Pulidindi
Member Level: Silver
Member Status: Member,MVP
Member Since: 12/30/2012 1:51:40 AM
Country: India
ramasagar
http://www.ramasagar.com
A Software Profesional working in Microsoft .NET technologies since year 2008, and I work for Dake ACE. I am passionate about .NET technology and love to contribute to the .NET community at Dot Net Funda

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)