ASP.NET Web API教程 创建域模型的方法详细介绍

添加模型
There are three ways to approach Entity Framework:
有三种方式使用实体框架:
Database-first: You start with a database, and Entity Framework generates the code.
Database-first(数据库先行):从一个数据库开始,然后实体框架生成相应代码。
Model-first: You start with a visual model, and Entity Framework generates both the database and code.
Model-first(模型先行):先从一个可视化模型开始,然后实体框架生成数据库和代码。
Code-first: You start with code, and Entity Framework generates the database.
Code-first(代码先行):先从代码开始,然后实体框架生成数据库。
We are using the code-first approach, so we start by defining our domain objects as POCOs (plain-old CLR objects). With the code-first approach, domain objects don't need any extra code to support the database layer, such as transactions or persistence. (Specifically, they do not need to inherit from the EntityObject class.) You can still use data annotations to control how Entity Framework creates the database schema.

我们打算使用code-first方法,因此,首先把域对象定义成POCO(plain-old CLR objects — 旧式无格式公共语言运行时(CLR)对象。很多人不太理解POCO对象,其实这种对象就像文本文件一样,是一种最简单、最原始、不带任何格式的对象。因此,在各种环境中最容易对这类对象进行处理,包括用各类语言进行处理 — 译者注)。利用code-first方法,域对象不需要任何附加代码去支持数据库层,如事务处理、持久化等。(特别是它们不需要继承于EntityObject类。)你仍可以使用数据注解(data annotation)对实体框架如何创建数据库方案进行控制。
Because POCOs do not carry any extra properties that describe database state, they can easily be serialized to JSON or XML. However, that does not mean you should always expose your Entity Framework models directly to clients, as we'll see later in the tutorial.
由于POCO不带描述数据库状态的任何附加属性,它们可以很容易地被序列化成JSON或XML。然而,这并不意味着你应当总是把实体框架模型直接暴露给客户端,就像我们稍后在本教程所看到的那样。

We will create the following POCOs:
我们将创建以下POCO:
Product
Order
OrderDetail
To create each class, right-click the Models folder in Solution Explorer. From the context menu, select Add and then select Class.
要创建每个类,在“解决方案资源管理器”中右击Models文件夹。从上下文菜单选择“添加”,然后选择“类”(如图2-14所示)。

WebAPI2-14

 
图2-14. 创建POCO类
Add a Product class with the following implementation:
用以下实现添加一个Product类(产品类):

复制代码 代码如下:


namespace ProductStore.Models
{
using System.ComponentModel.DataAnnotations;
public class Product
{
[ScaffoldColumn(false)]
public int Id { get; set; }
[Required]
public string Name { get; set; }
public decimal Price { get; set; }
public decimal ActualCost { get; set; }
}
}


By convention, Entity Framework uses the Id property as the primary key and maps it to an identity column in the database table. When you create a new Product instance, you won't set a value for Id, because the database generates the value.
根据约定,实体框架用Id属性作为主键,并把它映射成数据库表中的标识列。当创建一个新的Product实例时,不必为Id设置值,因为数据库会生成它。
The ScaffoldColumn attribute tells ASP.NET MVC to skip the Id property when generating an editor form. The Required attribute is used to validate the model. It specifies that the Name property must be a non-empty string.
ScaffoldColumn(支架列)注解属性是告诉ASP.NET MVC,在生成编辑表单时,跳过这个Id属性。Required注解属性用于对模型进行验证。它指定Name属性必须是一个非空字符串。
注:本文把ScaffoldConlumn、Required等这一类英文中叫做Annotation Attribute的属性(Attribute)译为注解属性(Annotation Attribute),以便与类中的那些属性加以区别 — 译者注
Add the Order class:
添加Order类(订单类):

复制代码 代码如下:


namespace ProductStore.Models
{
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public class Order
{
public int Id { get; set; }
[Required]
public string Customer { get; set; }
// Navigation property
// 导航属性
public ICollection<OrderDetail> OrderDetails { get; set; }
}
}


Add the OrderDetail class:
添加OrderDetail类(订单细节类,或订单详情类):

复制代码 代码如下:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjdxxw.html