国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Entity Framework + WCF REST JSON Service

2019-11-15 02:29:43
字體:
來源:轉載
供稿:網友
Entity Framework + WCF REST JSON Service

利用EF 和WCF 建立一個REST JSON Service. 首先我們要下載一個Visual Studio 的Template 叫 “ADO.NET C# POCO Entity Generator With WCF Support”.

1-29-2014 11-39-33 AM

這個主要是用于生成WCF的Model Class. 因為默認的EF 的Template是沒有[DataMember]和[DataContract]這個Annotation的。

建立一個Visual Studio 的PRoject.建立一個Entity framework EDMX。這里面我們有一個Table,

image

上面已經說過,默認的EF 4.0下生成的template是沒有[DataMember]和[DataContract]這個Annotation的,所以我們要用新的Template來生成Model class.

如果你打開Employee.cs的時候,你會發現class上面是沒有[DataContract],屬性是沒有DataMember的。

image

首先,我們先刪除自動生成的template和Model class

image

首先回到EDMX,右鍵Add Code Generation Item…

image

選擇 EF 5.x DbContext Generator with WCF Support

SNAGHTML5212d6

當我們加完之后,再看我們的Employee.cs

image

這里面要說一下,因為JSON不支持序列化IsReference這個屬性,所以如果你要輸出JSON的話,就需要刪除這個IsReference.如果你輸出時xml的話,IsReference是沒問題的。

所以我們要進到template文件,刪除這個IsReference,這個就很簡單了,走一個簡單的查詢就可以了。注意,在這個template中IsReference有兩處,記得全刪除就可以了

image

基本上,Entity Framework上JSON的問題已經完成了,下面就是寫Service了,我們就寫一個Service,GetEmployee(int employeID)

首先,我們創建一個EmployeeService.svc,

這里有一點注意,如果你用UriTemplate = “employee/{id}”的話,Employee GetEmployee(int id)這里,就必須是String id,否則的話他會拋異常

image

image

好了,最后就是web.config了

webconfig里面沒有什么,只要注意加一個endpointBehavior <webHttp />,然后你的service endpoint 里面behaviorConfiguration = 這個endpointBehavior.

還有就是你的service endpoint的binding type 是 webHttpBinding.

最后記得加mexHttpBinding

image

全部的web.config在這里

<?xml version="1.0" encoding="utf-8"?><configuration>  <configSections>    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />  </configSections>  <appSettings>    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />  </appSettings>  <system.web>    <compilation debug="true" targetFramework="4.5">      <assemblies>        <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />      </assemblies>    </compilation>    <httpRuntime targetFramework="4.5" />  </system.web>  <system.serviceModel>    <services>      <service name="WcfRestServiceSample.EmployeeService" behaviorConfiguration="serviceBehav">        <endpoint address="" binding="webHttpBinding" bindingConfiguration="" behaviorConfiguration="restfulBehaviour"          contract="WcfRestServiceSample.IEmployeeService" />        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />      </service>    </services>    <behaviors>      <endpointBehaviors>        <behavior name="restfulBehaviour">          <webHttp />        </behavior>      </endpointBehaviors>      <serviceBehaviors>        <behavior name="serviceBehav">          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />          <serviceDebug includeExceptionDetailInFaults="true" />        </behavior>      </serviceBehaviors>    </behaviors>    <protocolMapping>      <add binding="basicHttpsBinding" scheme="https" />    </protocolMapping>    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multWord=9ijn)OKM;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />  </connectionStrings>  <entityFramework>    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">      <parameters>        <parameter value="v11.0" />      </parameters>    </defaultConnectionFactory>  </entityFramework></configuration>

執行的結果是

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 合肥市| 安乡县| 互助| 华阴市| 泰和县| 奉化市| 澳门| 余干县| 大田县| 酒泉市| 盐城市| 冷水江市| 娄烦县| 仙游县| 怀柔区| 三江| 仁化县| 进贤县| 永靖县| 佛冈县| 密山市| 通化市| 开江县| 白城市| 双桥区| 曲阜市| 景东| 额济纳旗| 宜春市| 白山市| 岳普湖县| 卓资县| 句容市| 大宁县| 裕民县| 佳木斯市| 原平市| 辽阳市| 大理市| 尼木县| 乐东|