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.
分享到:
相关推荐
A Visual Studio 2013 project which shows how to use the Entity Framework 6 in an ASP.NET MVC 5 web application project, using the Code First development approach. The previous version that uses EF 5 ...
C# 7.1 and .NET Core 2.0 – Modern Cross-Platform Development – Third Edition 版本: Create powerful applications with .NET Standard 2.0, ASP.NET Core 2.0, … Visual Studio 2017 or Visual Studio Code ...
Entity Framework Core Cookbook - Second Edition by Ricardo Peres English | 9 Nov. 2016 | ISBN: 1785883305 | 340 Pages | MOBI/EPUB/PDF+Code Files | 6.2 MB Entity Framework is a highly recommended ...
A Visual Studio project which shows how to use the Entity Framework in an ASP.NET MVC web application project, using the Code First development approach.
You will explore how to use Entity Framework with ASP.NET Web API and also how to consume the data exposed by Entity Framework from client applications of varying types, i.e., ASP.NET MVC, WPF and ...
在ASP.NET MVC3中,可以使用Entity Framework的code-first方法创建数据库。这涉及到定义模型类,然后通过EF自动创建数据库结构。数据的获取和显示,以及编辑和验证,都可以通过控制器和视图的配合实现。 编辑数据时...
Code-First Development with Entity Framework 4 (data access) More flexible ACL implementation More user-friendly admin area Added unit tests Improvements Performance optimization reCAPTCHA ...
Code-First Development with Entity Framework 4 (data access) More flexible ACL implementation More user-friendly admin area Added unit tests Improvements Performance optimization reCAPTCHA ...
4. **ASP.NET MVC** MVC(Model-View-Controller)设计模式在ASP.NET中广泛使用。我们将深入理解模型、视图和控制器各自的职责,以及路由系统的工作原理。同时,会涉及到视图模型和razor语法的使用。 5. **Web服务...
QLDA(可能代表Queensland Learning ...同时,ASP.NET MVC和Entity Framework提供了强大的后端支持,确保数据的高效管理和流畅的业务流程。JQuery Ajax则增强了用户交互,使得数据的异步加载成为可能,提高了用户体验。
6. **Entity Framework 4.0**:数据库访问技术的重大升级,引入了模型第一和数据库第一的设计模式,支持Poco(Plain Old CLR Objects)实体,以及改进的Code First开发体验。 7. **Visual Studio 2010**:作为.NET ...