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

这个主要是用于生成WCF的Model Class. 因为默认的EF 的Template是没有[DataMember]和[DataContract]这个Annotation的。
建立一个Visual Studio 的project.建立一个Entity framework EDMX。这里面我们有一个Table,

上面已经说过,默认的EF 4.0下生成的template是没有[DataMember]和[DataContract]这个Annotation的,所以我们要用新的Template来生成Model class.
如果你打开Employee.cs的时候,你会发现class上面是没有[DataContract],属性是没有DataMember的。

首先,我们先删除自动生成的template和Model class

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

选择 EF 5.x DbContext Generator with WCF Support

当我们加完之后,再看我们的Employee.cs

这里面要说一下,因为JSON不支持序列化IsReference这个属性,所以如果你要输出JSON的话,就需要删除这个IsReference.如果你输出时xml的话,IsReference是没问题的。
所以我们要进到template文件,删除这个IsReference,这个就很简单了,走一个简单的查询就可以了。注意,在这个template中IsReference有两处,记得全删除就可以了

基本上,Entity Framework上JSON的问题已经完成了,下面就是写Service了,我们就写一个Service,GetEmployee(int employeID)
首先,我们创建一个EmployeeService.svc,
这里有一点注意,如果你用UriTemplate = “employee/{id}”的话,Employee GetEmployee(int id)这里,就必须是String id,否则的话他会抛异常


好了,最后就是web.config了
webconfig里面没有什么,只要注意加一个endpointBehavior <webHttp />,然后你的service endpoint 里面behaviorConfiguration = 这个endpointBehavior.
还有就是你的service endpoint的binding type 是 webHttpBinding.
最后记得加mexHttpBinding

全部的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" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true" />
</system.webServer>
<connectionStrings>
<add name="TechsharpSolutionEntities" connectionString="metadata=res://*/MyCompany.csdl|res://*/MyCompany.ssdl|res://*/MyCompany.msl;provider=System.Data.SqlClient;provider connection string="data source=localhost\Sql2008R2;initial catalog=TechsharpSolution;persist security info=True;user id=sa;password=9ijn)OKM;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
</configuration>
执行的结果是

Entity Framework + WCF REST JSON Service的更多相关文章
- Entity Framework + WCF 远程调用出错
在使用Entity Framework中使用WCF,在程序中调用服务一直报错,我一直以为是WCF的哪个地方的配置有问题,找来找去,一直没有解决. 最后在网上找到一篇文章 ...
- Entity framework在用于WCF时创建数据模型的问题
众所周知,WCF的传输对象,在创建时需要在类名上标识[DataContract]以及在属性上标识[DataMember],当我们在使用Entity framework时(不考虑Code first的情 ...
- Entity Framework在WCF中序列化的问题
问题描述 如果你在WCF中用Entity Framework来获取数据并返回实体对象,那么对下面的错误一定不陌生. 接收对 http://localhost:5115/ReService.svc 的 ...
- 精进不休 .NET 4.5 (12) - ADO.NET Entity Framework 6.0 新特性, WCF Data Services 5.6 新特性
[索引页][源码下载] 精进不休 .NET 4.5 (12) - ADO.NET Entity Framework 6.0 新特性, WCF Data Services 5.6 新特性 作者:weba ...
- 如何使用ASP.NET Web API OData在Oracle中使用Entity Framework 6.x Code-First方式开发 OData V4 Service
环境: Visual Studio 2013 + .Net Framework 4.5.2 1.新建项目 2.安装OData,ODP.NET 安装的包: 下面是部分代码: using System; ...
- JSON Support in PostgreSQL and Entity Framework
JSON 和JSONB的区别(What's difference between JSON and JSONB data type in PosgresSQL?) When should be use ...
- Windows Service 项目中 Entity Framework 无法加载的问题
Windows Service 项目引用了别的类库项目,别的项目用到了 Entity Framework(通过Nuget引入),但是我的 Windows Service 无法开启,于是我修改了 App ...
- Newtonsoft.Json高级用法,json序列号,model反序列化,支持序列化和反序列化DataTable,DataSet,Entity Framework和Entity,字符串
原文地址:https://www.cnblogs.com/yanweidie/p/4605212.html 手机端应用讲究速度快,体验好.刚好手头上的一个项目服务端接口有性能问题,需要进行优化.在接口 ...
- Entity Framework在WCF中序列化的问题(转)
问题描述 如果你在WCF中用Entity Framework来获取数据并返回实体对象,那么对下面的错误一定不陌生. 接收对 http://localhost:5115/ReService.svc 的 ...
随机推荐
- 【MyLocations】标记位置App开发体会
实现功能: 1.能通过Cora Location获取地址信息 2.用户获取地址信息后能编辑相关信息 3.使用Core Data保存数据 4.使用MapKit,在Map上显示标记的位置,并可以编辑位置信 ...
- 【SQL】SQL2012 导入导出报错,未在计算机上注册...
导出时报错: 如图: 解决方法:下载插件: 下载地址:http://download.microsoft.com/download/7/0/3/703ffbcb-dc0c-4e19-b0da-1463 ...
- PC/UVa 题号: 110106/10033 Interpreter (解释器)题解 c语言版
, '\n'); #include<cstdio> #include<iostream> #include<string> #include<algorith ...
- “WinMount”和“云端”真是相当好用!
WinMount作为一款压缩文件管理以及虚拟光驱工具已经无敌了.更有两项功能相当好用: 1.将rar.zip等压缩文件直接虚拟成磁盘,也就是下载一个7G的游戏可以不用解压直接安装了! 2.右键压缩文件 ...
- Java和C#在面向对象上语法的区别
做了几年了开发一直没有总结什么,回到了家乡的小城做了一名培训班的教员,教授软件开发的知识.细小的知识从头细细嚼来,别有一番滋味.或是以前遗漏的太多,或是确实没有系统的学习过,教学生的过程中自己也对教材 ...
- 【Bootstrap3.0建站笔记三】AspNetPager分页,每一列都可排序
1.AspNetPager分页,实现每一列都可排序: (1).须要将默认排序字段放在HTML页面中. (2).排序字段放置为td节点的属性. 如图: 实现的效果 ...
- sharepoint 2013 未能加载类型"Microsoft.AnalysisServices.SharePoint.Integration.ReportGalleryView"
最近在做PowerPivot for sharepoint server 2013的时候,创建PowerPivot库,之后打开,出现了一个问题: 未能加载类型"Microsoft.Analy ...
- [Angular 2] Inject Service with "Providers"
In this lesson, we’re going to take a look at how add a class to the providers property of a compone ...
- php生成CSV格式(转)
参考网址: php对csv文件的常用操作集合 http://blog.xhbin.com/archives/748 1,下载CSV格式文档 唯一需要特别注意的是编码. <? include_on ...
- C++之运算符重载(1)
在前一节中曾提到过,C++中运行时的多态性主要是通过虚函数来实现的,而编译时的多态性是由函数重载和运算符重载来实现的.这一系列我将主要讲解C++中有关运算符重载方面的内容.在每一个系列讲解之前,都会有 ...