Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Entity Framework Code First and WCF Data Services

, 10 Sep 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
Entity Framework combined with WCF Data Services provides you a fast approach to present your data. In combination with Entity Framework Code first it allows you to create simple applications, which shows a quick result and is also easy to maintain!

Entity Framework combined with WCF Data Services provides you a fast approach to present your data. In combination with Entity Framework Code First, it allows you to create simple applications, which shows a quick result and is also easy to maintain (also great for quick samples and mocks, because EF Code First generates the database for you). EF code first is part of part of EF CTP and will be added to the .NET Framework in a future version. The CTP can be downloaded here!

Entity Framework Code First allows you to create your model without any designer or XML. The tables will be mapped automatically to your custom classes if the class has the similar name as the table and the fields have the similar name as the columns. The real great thing is, when your database doesn't exist, EF will create it for you on the fly, just like magic! This is great if you create some mocks or samples.

public class ServerInfo
{
    public int ID { get; set; }
    public DateTime Startup { get; set; }
}

public class ServerContext: DbContext
{
    public DbSet ServerInfos{get; set;}
}

static void Main(string[] args)
{
    ServerContext conn = new ServerContext();
    conn.ServerInfos.Add(new ServerInfo() { Startup = DateTime.Now });
    conn.SaveChanges();
}

That's all the code you need. Now you can work with it like all other Entity Framework Context and Entities. But back to our goal, to create WCF Data Services combined with Entity Framework Code First (which isn't that far away now). Simply create an ASP.NET web application, add the two classes ServerContext and ServerInfo to the project. Additionally, add a WCF Data Service, which we will call ServerInfoService.

[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class ServerInfoService : DataService<ServerContext> 
{
    public static void InitializeService(DataServiceConfiguration config)
    {
        ServerContext conn = new ServerContext();
        conn.ServerInfos.Add(new ServerInfo() { Startup = DateTime.Now });
        conn.SaveChanges();

        config.SetEntitySetAccessRule("*", EntitySetRights.All);
    }
}

The ServerBehaviour is only added to see the error if you text your application. Every time the service is started, a new record to the database is added with the startup time. Now you can hit F5, select the ServerInfoService.svc and you should see a result in the form of an XML. Now we need a client project. Create a console application and add the service with "add service reference" (call the service reference also ServerInfoService).

static void Main(string[] args)
{
    var uri = new Uri("http://localhost:7640/ServerInfoService.svc");

    var context = new ServerInfoService.ServerContext(uri);

    foreach(var values in context.ServerInfos)
    {
        Console.WriteLine(values.Startup);
    }

    Console.ReadLine(); 
}

Copy the code into your Program.cs file and replace the port number with port number of your services. It iterates over all ServerInfo objects which are loaded from your database and prints the startup time to the console.

It is just great how fast and easily you are able to present some data which is stored in your database. Anyhow the combination of EF and WCF Data Services allows to create correctly layered applications without any additional effort.

Download the source code!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Mattia Baldinger
Software Developer
Switzerland Switzerland
No Biography provided
Follow on   Twitter

Comments and Discussions

 
QuestionHave a Five PinmemberDavid Roh19-Mar-13 8:37 
Sometimes a very simple app is the best way to get across some basic ideas quickly without having to wade through a lot of complex code - thanks.
GeneralMy vote of 1 PinmemberDaniel Nutu15-Sep-10 0:12 
GeneralMy vote of 2 PinmemberAndries Olivier13-Sep-10 21:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.141015.1 | Last Updated 10 Sep 2010
Article Copyright 2010 by Mattia Baldinger
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid