婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁(yè) > 知識(shí)庫(kù) > ASP.NET Web API教程 創(chuàng)建域模型的方法詳細(xì)介紹

ASP.NET Web API教程 創(chuàng)建域模型的方法詳細(xì)介紹

熱門標(biāo)簽:電話機(jī)器人廣告話術(shù) 太原外呼電銷機(jī)器人費(fèi)用 保山電話外呼管理系統(tǒng)怎么用 蘇州銷售外呼系統(tǒng)預(yù)算 淘寶地圖標(biāo)注如何做 使用智能電話機(jī)器人違法嗎 外呼系統(tǒng)用員工身份證 東莞語(yǔ)音電銷機(jī)器人排名 朝陽(yáng)市地圖標(biāo)注
添加模型
There are three ways to approach Entity Framework:
有三種方式使用實(shí)體框架:
Database-first: You start with a database, and Entity Framework generates the code.
Database-first(數(shù)據(jù)庫(kù)先行):從一個(gè)數(shù)據(jù)庫(kù)開(kāi)始,然后實(shí)體框架生成相應(yīng)代碼。
Model-first: You start with a visual model, and Entity Framework generates both the database and code.
Model-first(模型先行):先從一個(gè)可視化模型開(kāi)始,然后實(shí)體框架生成數(shù)據(jù)庫(kù)和代碼。
Code-first: You start with code, and Entity Framework generates the database.
Code-first(代碼先行):先從代碼開(kāi)始,然后實(shí)體框架生成數(shù)據(jù)庫(kù)。
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方法,因此,首先把域?qū)ο蠖x成POCO(plain-old CLR objects — 舊式無(wú)格式公共語(yǔ)言運(yùn)行時(shí)(CLR)對(duì)象。很多人不太理解POCO對(duì)象,其實(shí)這種對(duì)象就像文本文件一樣,是一種最簡(jiǎn)單、最原始、不帶任何格式的對(duì)象。因此,在各種環(huán)境中最容易對(duì)這類對(duì)象進(jìn)行處理,包括用各類語(yǔ)言進(jìn)行處理 — 譯者注)。利用code-first方法,域?qū)ο蟛恍枰魏胃郊哟a去支持?jǐn)?shù)據(jù)庫(kù)層,如事務(wù)處理、持久化等。(特別是它們不需要繼承于EntityObject類。)你仍可以使用數(shù)據(jù)注解(data annotation)對(duì)實(shí)體框架如何創(chuàng)建數(shù)據(jù)庫(kù)方案進(jìn)行控制。
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不帶描述數(shù)據(jù)庫(kù)狀態(tài)的任何附加屬性,它們可以很容易地被序列化成JSON或XML。然而,這并不意味著你應(yīng)當(dāng)總是把實(shí)體框架模型直接暴露給客戶端,就像我們稍后在本教程所看到的那樣。

We will create the following POCOs:
我們將創(chuàng)建以下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.
要?jiǎng)?chuàng)建每個(gè)類,在“解決方案資源管理器”中右擊Models文件夾。從上下文菜單選擇“添加”,然后選擇“類”(如圖2-14所示)。
 
圖2-14. 創(chuàng)建POCO類
Add a Product class with the following implementation:
用以下實(shí)現(xiàn)添加一個(gè)Product類(產(chǎn)品類):
復(fù)制代碼 代碼如下:

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.
根據(jù)約定,實(shí)體框架用Id屬性作為主鍵,并把它映射成數(shù)據(jù)庫(kù)表中的標(biāo)識(shí)列。當(dāng)創(chuàng)建一個(gè)新的Product實(shí)例時(shí),不必為Id設(shè)置值,因?yàn)閿?shù)據(jù)庫(kù)會(huì)生成它。
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,在生成編輯表單時(shí),跳過(guò)這個(gè)Id屬性。Required注解屬性用于對(duì)模型進(jìn)行驗(yàn)證。它指定Name屬性必須是一個(gè)非空字符串。
注:本文把ScaffoldConlumn、Required等這一類英文中叫做Annotation Attribute的屬性(Attribute)譯為注解屬性(Annotation Attribute),以便與類中的那些屬性加以區(qū)別 — 譯者注
Add the Order class:
添加Order類(訂單類):
復(fù)制代碼 代碼如下:

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
// 導(dǎo)航屬性
public ICollectionOrderDetail> OrderDetails { get; set; }
}
}

Add the OrderDetail class:
添加OrderDetail類(訂單細(xì)節(jié)類,或訂單詳情類):
復(fù)制代碼 代碼如下:

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
外鍵關(guān)系
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.
一份訂單包含很多訂單細(xì)節(jié),而每個(gè)訂單細(xì)節(jié)指向一個(gè)單一的產(chǎn)品。為了表示這些關(guān)系,OrderDetail類定義了名稱為OrderId和ProductId的屬性。實(shí)體框架將會(huì)推斷出這些屬性表示的是外鍵,并會(huì)把外鍵約束添加到數(shù)據(jù)庫(kù)(見(jiàn)圖2-15)。
 
圖2-15. 外鍵關(guān)系
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類也包含了“導(dǎo)航(navigation)”屬性,導(dǎo)航屬性包含了對(duì)相關(guān)對(duì)象的引用。對(duì)于一份給定的訂單,可以根據(jù)導(dǎo)航屬性導(dǎo)航到這份訂單的產(chǎn)品。
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.
現(xiàn)在,編譯這個(gè)項(xiàng)目。實(shí)體框架會(huì)使用反射來(lái)發(fā)現(xiàn)這些模型的屬性,因此它需要編譯后的程序集來(lái)創(chuàng)建相應(yīng)的數(shù)據(jù)庫(kù)方案(這里的數(shù)據(jù)庫(kù)方案意指數(shù)據(jù)庫(kù)、表結(jié)構(gòu)以及關(guān)系等數(shù)據(jù)庫(kù)方面的定義 — 譯者注)。
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書(shū)寫(xiě)HTTP響應(yīng)體時(shí)對(duì)數(shù)據(jù)進(jìn)行序列化的一個(gè)對(duì)象。內(nèi)建的格式化器支持JSON和XML輸出。默認(rèn)地,這兩種格式化都會(huì)按值序列化所有對(duì)象。
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.
如果對(duì)象圖含有循環(huán)引用,按值序列化會(huì)出現(xiàn)問(wèn)題。這恰好是Order類和OrderDetail類的情況,因?yàn)槊恳粋€(gè)都含有對(duì)另一個(gè)的引用。格式化器會(huì)遵循這些引用,按值寫(xiě)出每一個(gè)對(duì)象,于是會(huì)引起循環(huán)。因此,我們需要修改這種默認(rèn)行為。
In Solution Explorer, expand the App_Start folder and open the file named WebApiConfig.cs. Add the following code to the WebApiConfig class:
在“解決方案資源管理器”中,展開(kāi)App_Start文件夾,并打開(kāi)名為WebApiConfig.cs的文件。將以下代碼添加到這個(gè)WebApiConfig.cs類中(以下代碼中的“新代碼” — 譯者注):
復(fù)制代碼 代碼如下:

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格式化器設(shè)置為防止對(duì)象引用(“新代碼”第二行的作用 — 譯者注),并把XML格式化器從管線(指HTTP的請(qǐng)求處理管線 — 譯者注)中完全刪除(“新代碼”最后一行的作用 — 譯者注)。(你也可以把XML格式化器配置成防止對(duì)象引用,但這還要做一點(diǎn)工作,而對(duì)于這個(gè)應(yīng)用程序,我們只需要JSON。更多信息參閱“處理循環(huán)對(duì)象引用”
您可能感興趣的文章:
  • 創(chuàng)建一個(gè)完整的ASP.NET Web API項(xiàng)目
  • ASP.NET中Web API的簡(jiǎn)單實(shí)例
  • ASP.NET MVC Web API HttpClient簡(jiǎn)介
  • ASP.NET Web Api 2實(shí)現(xiàn)多文件打包并下載文件的實(shí)例
  • 支持Ajax跨域訪問(wèn)ASP.NET Web Api 2(Cors)的示例教程
  • ASP.NET Web API教程 創(chuàng)建Admin視圖詳細(xì)介紹
  • ASP.NET Web API如何將注釋自動(dòng)生成幫助文檔
  • ASP.NET Web API教程 創(chuàng)建Admin控制器實(shí)例分享
  • .Net Web Api中利用FluentValidate進(jìn)行參數(shù)驗(yàn)證的方法

標(biāo)簽:潛江 阿里 運(yùn)城 綏化 克拉瑪依 西藏 洛陽(yáng) 呼倫貝爾

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《ASP.NET Web API教程 創(chuàng)建域模型的方法詳細(xì)介紹》,本文關(guān)鍵詞  ASP.NET,Web,API,教程,創(chuàng)建,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《ASP.NET Web API教程 創(chuàng)建域模型的方法詳細(xì)介紹》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于ASP.NET Web API教程 創(chuàng)建域模型的方法詳細(xì)介紹的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av
    成人国产视频在线观看| 欧美精选一区二区| 亚洲一区二区三区在线看| 欧美mv日韩mv国产网站| 国产精品美女久久久久aⅴ| 国产精品毛片高清在线完整版| 国产精品萝li| 免费一区二区视频| 成人影视亚洲图片在线| 成人av网站免费观看| 一本色道久久综合亚洲91| 欧美美女网站色| 国产福利91精品一区| 国产iv一区二区三区| 欧美理论片在线| 精品国产一区二区亚洲人成毛片 | 精品少妇一区二区三区日产乱码| 一区二区欧美国产| 欧美日韩大陆一区二区| 韩日精品视频一区| 亚洲欧美日韩一区| 91精品视频网| 国产精品一区专区| 亚洲啪啪综合av一区二区三区| 欧洲一区二区av| 国产综合色精品一区二区三区| 国产精品网曝门| 欧美三级电影网站| 日韩欧美国产综合| 欧美一级午夜免费电影| 一区二区成人在线视频| 国产精品福利一区二区三区| 亚洲综合久久久久| 老司机午夜精品| 色8久久精品久久久久久蜜| 免费美女久久99| 亚洲va欧美va天堂v国产综合| 国产精品家庭影院| 亚洲青青青在线视频| 亚洲美腿欧美偷拍| 国产精品成人免费精品自在线观看 | 国产不卡视频一区二区三区| 国产一区美女在线| 精品一区二区三区影院在线午夜| 亚洲高清一区二区三区| 亚洲国产精品精华液网站| 亚洲欧美一区二区三区国产精品| 成人欧美一区二区三区小说| 亚洲综合成人网| 亚洲人吸女人奶水| 亚洲午夜精品17c| 亚洲va欧美va人人爽| 日本视频在线一区| 韩国精品在线观看| 一本到高清视频免费精品| 欧美日韩中文另类| 精品国免费一区二区三区| 国产精品久久毛片| 亚洲成人av在线电影| 久久精品噜噜噜成人av农村| 91视频观看视频| 成人av综合一区| 欧美一区二区三区精品| 久久久一区二区| 亚洲精品水蜜桃| 国产精品一区二区视频| 91高清视频免费看| 精品国产a毛片| 一区二区三区四区在线播放 | 日韩欧美精品在线视频| 亚洲情趣在线观看| 在线视频你懂得一区二区三区| 亚洲欧洲性图库| 色综合久久久久综合体| 亚洲女人****多毛耸耸8| 欧美在线不卡一区| 日本色综合中文字幕| 国产精品青草久久| 亚洲一区免费在线观看| 播五月开心婷婷综合| www国产成人| 激情六月婷婷综合| 精品区一区二区| 国产高清不卡二三区| 久久综合中文字幕| 国产一区日韩二区欧美三区| 日韩欧美精品在线视频| 乱中年女人伦av一区二区| 色先锋资源久久综合| 国产精品久久久久久亚洲毛片 | 国产视频在线观看一区二区三区| 日日摸夜夜添夜夜添国产精品| 9色porny自拍视频一区二区| 亚洲欧美日韩综合aⅴ视频| 欧美电影一区二区| 久久99国产精品久久| 欧美综合一区二区三区| 国模无码大尺度一区二区三区| 中文字幕在线播放不卡一区| 不卡的av电影在线观看| 日韩激情在线观看| 亚洲欧美日韩国产手机在线| 欧美一区二区高清| 成人激情电影免费在线观看| 亚洲mv在线观看| 中文字幕一区视频| 久久久99久久精品欧美| 制服丝袜在线91| 精品视频色一区| 欧美日韩国产美| 欧美在线free| 色呦呦日韩精品| 日本高清视频一区二区| 91蝌蚪porny九色| 国产精品99久久久久久有的能看 | 一本到不卡精品视频在线观看 | 成人做爰69片免费看网站| 免费不卡在线观看| 国产主播一区二区| 风流少妇一区二区| 岛国精品在线观看| 91在线观看高清| 欧美在线综合视频| 欧美一级艳片视频免费观看| 精品免费视频一区二区| 久久久欧美精品sm网站| 一色屋精品亚洲香蕉网站| 亚洲电影第三页| 狠狠色狠狠色合久久伊人| 国内不卡的二区三区中文字幕| 精品影视av免费| 91麻豆高清视频| 91在线视频观看| 欧美日韩三级一区| 精品国内二区三区| 亚洲美女在线国产| 精品制服美女久久| 一本色道久久加勒比精品| 3d成人动漫网站| 亚洲免费在线电影| 国产精品一区二区免费不卡| 欧美性色综合网| 亚洲精品久久久蜜桃| 国产一区二区精品在线观看| 欧美私模裸体表演在线观看| 日本一区二区在线不卡| 麻豆91在线看| 欧美精品乱码久久久久久按摩| 国产精品欧美一区喷水| 久国产精品韩国三级视频| 欧美丰满嫩嫩电影| 亚洲综合小说图片| 在线观看不卡一区| 一区二区三区四区不卡在线 | 亚洲国产精品一区二区www在线| 国产不卡视频在线观看| 国产三级一区二区三区| 国产一区91精品张津瑜| 91精品在线观看入口| 三级精品在线观看| 精品三级av在线| 91丨porny丨国产| 亚洲小少妇裸体bbw| 日韩一本二本av| 国产精品亚洲专一区二区三区| 国产精品日日摸夜夜摸av| 日本韩国一区二区| 亚洲第一二三四区| 久久久久久久久99精品| 99re这里只有精品首页| 亚洲国产精品久久久久秋霞影院| 欧美美女一区二区在线观看| 国产精品资源在线| 国内精品嫩模私拍在线| 国产亚洲精品久| 欧美日韩精品久久久| 国产综合久久久久久久久久久久| 亚洲欧美日韩综合aⅴ视频| 欧美tk丨vk视频| 欧美日韩精品免费观看视频| 成人伦理片在线| 国产成人精品影院| 日本不卡在线视频| 1024精品合集| 中文字幕永久在线不卡| 国产日韩欧美亚洲| 精品99久久久久久| 欧美日韩视频在线第一区| 91官网在线观看| 在线观看av不卡| 欧美区在线观看| 丁香天五香天堂综合| 成人精品鲁一区一区二区| 美国三级日本三级久久99| 日韩国产精品久久久久久亚洲| 午夜精品福利一区二区三区av| 中文字幕一区av| 亚洲一区av在线| 日韩精品福利网| 亚洲一区二区影院|