Through this tutorial, you will learn how to use asp.net mvc through the Entity Framework. You will learn to use the entity Wizard to create an ado.net Entity Data Model. In this tutorial, we will create a web application to show how to use the Entity Framework to display, insert, update, and delete database data.
Microsoft Entity Framework is an object relationship ing tool that allows you to create a data access layer. Avoid the trouble of manually creating a data category.
To demonstrate how to use Entity framework. We will create a simple application. Here we create a movie database application that allows you to display and edit movie database records.
This tutorial assumes that you have vs2008, visual web developer 2008, and service pack 1. To use entity framework, you need service pack 1. You can download Visual Studio 2008 Service Pack 1 or Visual Web Developer with Service Pack 1 from http://www.asp.net/downloads.
Note:
ASP. net mvc is not necessarily related to the Microsoft Entity Framework. In addition to the Entity Framework, you can also select other O/RM tools (such as Microsoft LINQ to SQL, NHibernate, or SubSonic ).
Then we need to create a database and the corresponding model (the specific content is omitted ).
Note:
Do not write code in the designer. cs file under the model folder, which is prone to errors. If you want to extend the entity class methods in designer. cs, you can writePartial classes.
Use entity framework to select data records
The following index () action returns records in all databases through entity framework:
Listing 1-ControllersHomeController. cs
Using System. Linq;
Using System. Web. Mvc;
Using MovieEntityApp. Models;
Namespace MovieEntityApp. Controllers
{
[HandleError]
Public class HomeController: Controller
{
MoviesDBEntities _ db;
Public HomeController ()
{
_ Db = new MoviesDBEntities ();
}
Public ActionResult Index ()
{
ViewData. Model = _ db. MovieSet. ToList ();
Return View ();
}
}
}
Note that there is a constructor in listing 1, which is used to initialize the field _ db in the class. _ Db represents the entity generated using entityframework. The _ db domain is an instance of the MoviesDBEntities class created in the model just now.
Note:
To use the theMoviesDBEntities class in the controler, you must introduce the MovieEntityApp. Models namespace (the space of theMoviesDBEntities ).
_ Db is used to return records from the database table in the index () action. Tolist () converts a movie dataset to a general movie objects set (List <movie> ).
The returned dataset is implemented using the LINQ to entities technique. The above listing 1 uses the LINQ method syntax to return the dataset. If you like it, you can also use the LINQ query syntax. Below are the same implementations of the two syntaxes:
ViewData. Model = _ db. MovieSet. ToList ();
ViewData. Model = (from m in _ db. MovieSet select m). ToList ();
Which of the following statements do you think is intuitive to use. There is no difference in their execution. The only difference is that the form is different.
The following listing 2 is used to display extracted records:
Listing 2-ViewsHomeIndex. aspx
<% @ Page Language = "C #"
Inherits = "System. Web. Mvc. ViewPage <List <MovieEntityApp. Models. Movie>" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> Index </