WCF REST服务(JSON),并首先使用我的模型数据与实体框架数据库 [英] WCF REST service (JSON) and using my model data with entity framework database first

查看:196
本文介绍了WCF REST服务(JSON),并首先使用我的模型数据与实体框架数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

另外一个相当大的冠军。

Another rather large title..

只是一些基本的信息:使用.NET 4 / EF 4.x版/的Visual Studio 2010 SP1

Just some basic info first: using .NET 4 / EF 4.x / visual studio 2010 sp1.

我这是使用JSON作为数据传输一个WCF基于REST的服务。我已经使用的例子(肯定还有其他的说明这一点)从史蒂夫Mitchelotti <一href="http://geekswithblogs.net/michelotti/archive/2010/08/21/restful-wcf-services-with-no-svc-file-and-no-config.aspx" rel="nofollow">http://geekswithblogs.net/michelotti/archive/2010/08/21/restful-wcf-services-with-no-svc-file-and-no-config.aspx

I have a WCF REST based service which is using JSON as the data transport. I have used the example (sure there are others describing this too) from Steve Mitchelotti http://geekswithblogs.net/michelotti/archive/2010/08/21/restful-wcf-services-with-no-svc-file-and-no-config.aspx

我有一个非常基本的模型/类,如下所示:

I have a very basic model/class as follows:

public class DiaryEvent
{ 
    public Int64 ID { get; set; }
    public Int64 CalendarResourceID { get; set; }
    public string Subject { get; set; }
    public string Location { get; set; }
    public DateTime Start { get; set; }
    public DateTime End { get; set; }
    public string Description { get; set; }
    public string Colour { get; set; }
    public string AllDay { get; set; }
}

现在,再以最小的WCF / REST的史蒂夫的例子​​,我有一个接口,它定义如下方法/数据服务合同:

For now, again with Steve's example of minimal WCF/REST, I have an interface which defines the methods/data service contracts as below:

[的ServiceContract] 接口IDiaryService {     [OperationContract的]     //列出GetEvents();     字符串GetEvents();

[ServiceContract] interface IDiaryService { [OperationContract] //List GetEvents(); string GetEvents();

[OperationContract]
DiaryEvent GetEvent(string id);

[OperationContract]
void InsertEvent(DiaryEvent NewDiary);

[OperationContract]
string UpdateEvent(string id, DiaryEvent UpdatedEventData);

}

我想我还可以添加其他WebGet / WebInvoke装修材料进入界面也是如此,但现在这是它的外观。

I guess I could also add the other WebGet/WebInvoke decoration stuff into the interface too, but for now that is how it looks.

所以后来就到了实际的服务code,而现在我使用的是基本的列表和LT;>管道一些测试数据上的getevents方法,像这样:

So then on to the actual service code, and for now I am using a basic list<> to pipe some test data on the getevents method like so:

WebGet(UriTemplate = "GetEvents", ResponseFormat=WebMessageFormat.Json)
public DiaryEvent GetEvents()
{
    List<DiaryEvent> EventList = new List<DiaryEvent>();
    EventList.Add(new DiaryEvent { AllDay = "false", ID = 1, CalendarResourceID = 1, Start = new DateTime(2012, 07, 18, 15, 0, 0), End = new DateTime(2012, 07, 18, 17, 0, 0), Location = "Golf Club 1", Colour = "#FEDEFF", Description = "Event 1 Description", Subject = "Event 1" });
    EventList.Add(new DiaryEvent { AllDay = "false", ID = 2, CalendarResourceID = 2, Start = new DateTime(2012, 07, 19, 15, 0, 0), End = new DateTime(2012, 07, 18, 17, 0, 0), Location = "Golf Club 1", Colour = "#FEDEFF", Description = "Event 2 Description", Subject = "Event 2" });
    EventList.Add(new DiaryEvent { AllDay = "false", ID = 3, CalendarResourceID = 3, Start = new DateTime(2012, 07, 20, 15, 0, 0), End = new DateTime(2012, 07, 18, 17, 0, 0), Location = "Golf Club 1", Colour = "#FEDEFF", Description = "Event 3 Description", Subject = "Event 3" });

    return EventList;
    //return JsonConvert.SerializeObject(EventList);
}

(只注意到计算器与[周围的装饰格式]有问题!)

(just notice stackoverflow has a problem with [ ] around the decoration formatting!)

所以显然是一个非常基本的例子,上面得到一些JSON数据返回给客户端。

So obviously a very basic example above to get some JSON data back to the client.

我的意思是,我是想用EF 4.x的也许是为了连接到我的数据库(已创建表,以便数据库首先将是我的选择..)。我只是需要一些指导如何最好地编写连接code来填充我的DiaryEvents模型,并从该EF数据?

My point is that I was wanting to use EF 4.x perhaps to connect to my database (tables already created so database first would be my option..). I just need some direction how best to write the connecting code to populate my DiaryEvents model to and from that EF data?

如果任何人都可以在正确的方向指向我的一些例子/想法??

If anyone could point me in the right direction for some examples/thoughts??

非常AP preciated! 大卫。

Very appreciated! David.

推荐答案

我认为你应该从这个code工程实例启动。

I think you should start from this code project example.

http://www.$c$cproject.com/Articles/127395/Implementing-a-WCF-Service-with-Entity-Framework

这篇关于WCF REST服务(JSON),并首先使用我的模型数据与实体框架数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆