2012-07-19 18 views
0

另一個相當大的冠軍。WCF REST服務(JSON),並使用我的模型數據與實體框架數據庫第一

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

我有一個基於WCF REST的服務使用JSON作爲數據傳輸。我從史蒂夫Mitchelotti使用的例子(肯定還有其他的描述這個太)http://geekswithblogs.net/michelotti/archive/2010/08/21/restful-wcf-services-with-no-svc-file-and-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; } 
} 

現在,再次以最小的WCF的史蒂夫的例子/ REST,我有定義了方法/數據服務,如下合同的接口:

[的ServiceContract] 接口IDiaryService { [OperationContract的] //列表GetEvents(); string 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); 
} 

(只注意到計算器有問題與[]周圍的裝飾格式化!)

所以顯然是一個非常基本的例子,以獲得一些JSON數據返回到客戶端。

我的觀點是,我想要使用EF 4.x或許連接到我的數據庫(表已經創建,所以數據庫第一將是我的選擇..)。我只需要一些指導,以便如何最好地編寫連接代碼,以便將EF數據中的DiaryEvents模型填充到該EF模型中;

如果有人能指出我在正確的方向爲一些例子/想法?

非常感謝! David。

回答

-1
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 
    } 
相關問題