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

本文是小编为大家收集整理的关于WCF REST服务(JSON),先用实体框架数据库使用我的模型数据的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

本文来自:IT宝库(https://www.itbaoku.cn)

另一个相当大的标题..

首先只是一些基本信息:使用.NET 4/EF 4.x/Visual Studio 2010 Sp1.

我有一个基于WCF REST的服务,该服务将JSON用作数据传输.我已经使用了示例(确保也有其他描述)来自Steve Mitchelotti http://geekswithblogs.net/michelotti/michelotti/archive/2010/08/08/21/restful-wcf-wcf-services-with-no-no-svc-file--svc-file---file---和no-config.aspx

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

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; }
}

目前,史蒂夫(Steve)的最小WCF/REST的示例再次,我有一个接口,该接口定义了方法/数据服务合同如下:

[serviceCntract] 界面iDiaryService { [coperation Contract] //列表getEvents(); 字符串getevents();

[OperationContract]
DiaryEvent GetEvent(string id);

[OperationContract]
void InsertEvent(DiaryEvent NewDiary);

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

}

我想我也可以将其他webget/webinvoke装饰物添加到界面中,但是现在这就是外观.

因此,然后使用实际的服务代码,目前我正在使用基本列表<>对类似getEvents方法进行一些测试数据:

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);
}

(只需注意,stackoverflow在装饰格式周围的[]都有问题!)

显然,上面是一个非常基本的示例,可以将一些JSON数据返回客户端.

我的观点是我想使用EF 4.x连接到我的数据库(表已经创建的表,因此数据库首先是我的选项..).我只需要一些方向,如何最好地编写连接代码来填充我的日记events模型和从EF数据?

如果有人可以将我指向某些示例/想法的正确方向?

非常感谢! 大卫.

推荐答案

我认为您应该从这个代码项目示例开始.

http:///www.codeproject. com/articles/127395/实施-a-wcf-service-with-with-entity-framework

其他推荐答案

ServiceContract]
    public interface IRestServiceImpl
    {
        [OperationContract]
        [System.ServiceModel.Web.WebInvoke(Method = "GET",ResponseFormat=System.ServiceModel.Web.WebMessageFormat.Xml, BodyStyle =System.ServiceModel.Web.WebMessageBodyStyle.Wrapped, UriTemplate = "xml/{id}")]
        string XMLData(string id);
        [OperationContract]
        [System.ServiceModel.Web.WebInvoke(Method = "GET", ResponseFormat = System.ServiceModel.Web.WebMessageFormat.Json, BodyStyle = System.ServiceModel.Web.WebMessageBodyStyle.Wrapped, UriTemplate = "json/{id}")]
        string JSONData(string id);
    }

public class RestServiceImpl : IRestServiceImpl
    {
        #region IRestService Members
        public string XMLData(string id)
        {
            return "You Request Porduct" + ":"+id;

        }
        public string JSONData(string id)
        {
            return "Yor Request Product" +":"+ id;
        }
        #endregion
    }

本文地址:https://www.itbaoku.cn/post/25929.html

问题描述

Another rather large title..

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

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; }
}

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] interface IDiaryService { [OperationContract] //List GetEvents(); string GetEvents();

[OperationContract]
DiaryEvent GetEvent(string id);

[OperationContract]
void InsertEvent(DiaryEvent NewDiary);

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

}

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

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!)

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

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??

Very appreciated! David.

推荐答案

I think you should start from this code project example.

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

其他推荐答案

ServiceContract]
    public interface IRestServiceImpl
    {
        [OperationContract]
        [System.ServiceModel.Web.WebInvoke(Method = "GET",ResponseFormat=System.ServiceModel.Web.WebMessageFormat.Xml, BodyStyle =System.ServiceModel.Web.WebMessageBodyStyle.Wrapped, UriTemplate = "xml/{id}")]
        string XMLData(string id);
        [OperationContract]
        [System.ServiceModel.Web.WebInvoke(Method = "GET", ResponseFormat = System.ServiceModel.Web.WebMessageFormat.Json, BodyStyle = System.ServiceModel.Web.WebMessageBodyStyle.Wrapped, UriTemplate = "json/{id}")]
        string JSONData(string id);
    }

public class RestServiceImpl : IRestServiceImpl
    {
        #region IRestService Members
        public string XMLData(string id)
        {
            return "You Request Porduct" + ":"+id;

        }
        public string JSONData(string id)
        {
            return "Yor Request Product" +":"+ id;
        }
        #endregion
    }