ASP.NET MVC 3 (Entity Framework Code-First Development) (4/9)

來源:互聯網
上載者:User
文章目錄
  • Entity Framework Code-First Development
  • Using NuGet to Install EFCodeFirst
  • Adding a Code-First POCO Class
Entity Framework Code-First Development

The Entity Framework version 4 supports a development paradigm called code-first. Code-first allows you to create model object by writing simple classes (also known as POCO, from "plain-old CLR objects") and have the database created on the fly from your classes. In order to use code-first, you must install the EFCodeFirst library.

Using NuGet to Install EFCodeFirst

In this section, you'll use the NuGet package manager (automatically installed by ASP.NET MVC 3) to add the EFCodeFirst library to the MvcMovie project.

From the Tools menu, select Library Package Manager and then Add Library Package Reference.

The Add Library Package Reference dialog box appears.

By default, All is selected in the left pane. Because no packages are installed, the center pane shows No items found. Click Online in the left pane.

NuGet queries the server for all available packages.

There are hundreds of packages available. We're interested in the EFCodeFirst package. In the search box, enter "EFCode", and then select the EFCodeFirst package and click the Install button.

After the package installs, click Close. The installation process downloaded the EFCodeFirst library and added it to the MvcMovie project. The EFCodeFirst library is contained in the EntityFramework assembly.

Adding a Code-First POCO Class

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

Name the class "Movie".

Add the following code to create the Movie class:

public class Movie  {     public int ID { get; set; }     public string Title { get; set; }     public DateTime ReleaseDate { get; set; }     public string Genre { get; set; }     public decimal Price { get; set; } }

In the same file, add the following MovieDBContext class:

 public class MovieDBContext : DbContext  {     public DbSet<Movie> Movies { get; set; }  } 

The MovieDBContext class represents the Entity Framework movie database context. The MovieDBContext class handles fetching, storing, and updating Movie class instances from a database. The complete Movie.cs file is shown below.

using System; using System.Data.Entity;  namespace MvcMovie.Models   {      public class Movie     {         public int ID { get; set; }         public string Title { get; set; }         public DateTime ReleaseDate { get; set; }         public string Genre { get; set; }         public decimal Price { get; set; }     }      public class MovieDBContext : DbContext      {         public DbSet<Movie> Movies { get; set; }      }  }

Our application has not connected to a database yet. In traditional web application development, we would create a database first, then write code to connect to the database. However, here we're using the code-first approach, so we've written our code (models) first. Later in the tutorial when you run the application, the code will create a database and movie table on the fly.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.