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


namespace ProductStore.Models
{
public class OrderDetail
{
public int Id { get; set; }
public int Quantity { get; set; }
public int OrderId { get; set; }
public int ProductId { get; set; }
// Navigation properties
public Product Product { get; set; }
public Order Order { get; set; }
}
}


Foreign Key Relations
外键关系
An order contains many order details, and each order detail refers to a single product. To represent these relations, the OrderDetail class defines properties named OrderId and ProductId. Entity Framework will infer that these properties represent foreign keys, and will add foreign-key constraints to the database.
一份订单包含很多订单细节,而每个订单细节指向一个单一的产品。为了表示这些关系,OrderDetail类定义了名称为OrderId和ProductId的属性。实体框架将会推断出这些属性表示的是外键,并会把外键约束添加到数据库(见图2-15)。

WebAPI2-15

 
图2-15. 外键关系
The Order and OrderDetail classes also include “navigation” properties, which contain references to the related objects. Given an order, you can navigate to the products in the order by following the navigation properties.
Order和OrderDetail类也包含了“导航(navigation)”属性,导航属性包含了对相关对象的引用。对于一份给定的订单,可以根据导航属性导航到这份订单的产品。
Compile the project now. Entity Framework uses reflection to discover the properties of the models, so it requires a compiled assembly to create the database schema.
现在,编译这个项目。实体框架会使用反射来发现这些模型的属性,因此它需要编译后的程序集来创建相应的数据库方案(这里的数据库方案意指数据库、表结构以及关系等数据库方面的定义 — 译者注)。
Configure the Media-Type Formatters
配置Media-Type格式化器
A media-type formatter is an object that serializes your data when Web API writes the HTTP response body. The built-in formatters support JSON and XML output. By default, both of these formatters serialize all objects by value.
media-type(媒体类型)格式化器是Web API书写HTTP响应体时对数据进行序列化的一个对象。内建的格式化器支持JSON和XML输出。默认地,这两种格式化都会按值序列化所有对象。
Serialization by value creates a problem if an object graph contains circular references. That's exactly the case with the Order and OrderDetail classes, because each holds a reference to the other. The formatter will follow the references, writing each object by value, and go in circles. Therefore, we need to change the default behavior.
如果对象图含有循环引用,按值序列化会出现问题。这恰好是Order类和OrderDetail类的情况,因为每一个都含有对另一个的引用。格式化器会遵循这些引用,按值写出每一个对象,于是会引起循环。因此,我们需要修改这种默认行为。
In Solution Explorer, expand the App_Start folder and open the file named WebApiConfig.cs. Add the following code to the WebApiConfig class:
在“解决方案资源管理器”中,展开App_Start文件夹,并打开名为WebApiConfig.cs的文件。将以下代码添加到这个WebApiConfig.cs类中(以下代码中的“新代码” — 译者注):

复制代码 代码如下:


public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
// New code:
// 新代码:
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling =
Newtonsoft.Json.PreserveReferencesHandling.Objects;
config.Formatters.Remove(config.Formatters.XmlFormatter);
}
}


This code sets the JSON formatter to preserve object references, and removes the XML formatter from the pipeline entirely. (You can configure the XML formatter to preserve object references, but it's a little more work, and we only need JSON for this application. For more information, see Handling Circular Object References.)
这段代码把JSON格式化器设置为防止对象引用(“新代码”第二行的作用 — 译者注),并把XML格式化器从管线(指HTTP的请求处理管线 — 译者注)中完全删除(“新代码”最后一行的作用 — 译者注)。(你也可以把XML格式化器配置成防止对象引用,但这还要做一点工作,而对于这个应用程序,我们只需要JSON。更多信息参阅“处理循环对象引用”

您可能感兴趣的文章:

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

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