WCF REST service (JSON) and using my model data with entity framework database first

.net c# entity-framework rest wcf

Question

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.

1
0
7/19/2012 9:53:27 AM

Accepted Answer

1
7/19/2012 11:43:39 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow