Login

Username:

Password: 

Join us Now |  Forgot Password? | Forgot UserName?

Asp.Net MVC

Learn step by step



ASP.NET MVC - Model

In MVC, the M stands for model, and it is the very important part of the application. The model is the illustration of the real-world objects, processes, and rules that explain the subject, known as the domain, of our application. The model, often mention to as a domain model, holds the C# objects that make up the global of our application and the methods that let us handle them. The views and controllers display the domain to our clients in a regular manner and a well-designed MVC application begin with a well-designed model, which is then the focus point as we add controllers and views.

The Models Folder

The Models Folder keep the classes that describe the application model.
Visual Web Developer automatically makes an AccountModels.cs file that hold the models for application security.
AccountModels keep a ChangePasswordModel, a LogOnModel, and a RegisterModel.

Adding a Model

We will use a .NET Framework data-access technology known as the Entity Framework(also referred as EF)to describe and work with these model classes. The Entity Framework (also referred as EF) assist a development paradigm called Code First. Code First allows you to make model objects by writing simple classes. (Also known as POCO classes("plain-old CLR objects.")) We can then have the database maked on the fly from your classes, which enables a very simple and rapid development work flow.

Adding Model Classes

In Solution Explorer,on Models folder right click,Choose Add, and then select Class.


Enter the class  name "TutOnDemo".

Add the following five properties to the TutOnDemo class:

public class TutOnDemo
{
public int ID { get; set; }
public string Title { get; set; }
public DateTime Date { get; set; }
public string Genre { get; set; }
public decimal Price { get; set; }
}

We will use the TutOnDemo class to describe TutOnDemo in a database. Every instance of a TutOnDemo object will correspond to a row within a database table, and every property of the TutOnDemo class will map to a column in the table.

In the same file, add the following TutOnDemoDBContext class:

public class TutOnDemoDBContext : DbContext
{
public DbSet<TutOnDemo> TutOnDemo { get; set; }
}

The TutOnDemoDBContext class describe the Entity Framework TutOnDemo database context, which manage fetching, saving, and updating TutOnDemo class instances in a database. The TutOnDemoDBContext derives from the DbContext base class provided by the Entity Framework.

In order to be able to reference DbContext and DbSet, we require to add the following using statement at the top of the file:

using System.Data.Entity;

The complete TutOnDemo.cs file is shown below.

using System;
using System.Data.Entity;

namespace TutOnMVC.Models
{
public class TutOnDemo
{
public int ID { get; set; }
public string Title { get; set; }
public DateTime Date { get; set; }
public string Genre { get; set; }
public decimal Price { get; set; }
}

public class TutOnDemoDBContext : DbContext
{
public DbSet<TutOnDemo> TutOnDemo { get; set; }
}
}
Making a Connection String and Working with SQL Server LocalDB

The TutOnDemoDBContext class you generated manage the task of connecting to the database and mapping TutOnDemo objects to database records. One question arise, is how to specify which database it will connect to.we will do that by adding connection information in the Web.config file of the application.

Open the application root Web.config file. (Not the Views folder Web.config file.) Open the Web.config file outlined in red.


Add the following connection string to the <connectionStrings> element in the Web.config file.

<add name="TutOnDemoDBContext"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\TutOnDemo.mdf;Integrated Security=True"
providerName="System.Data.SqlClient"
/>

The following example display a part of the Web.config file with the new connection string added:

<connectionStrings>
<add name="DefaultConnection"
connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-TutOnDemo-2012213181139;Integrated Security=true"
providerName="System.Data.SqlClient"
/>
<add name="TutOnDemoDBContext"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\TutOnDemo.mdf;Integrated Security=True"
providerName="System.Data.SqlClient"
/>
</connectionStrings>


Related Videos