Click here to Skip to main content
15,881,172 members
Articles / Programming Languages / C# 4.0

Entity Framework Code First and WCF Data Services

Rate me:
Please Sign up or sign in to vote.
2.67/5 (3 votes)
10 Sep 2010CPOL2 min read 21.9K   5   3
Entity Framework Code First and WCF Data Services

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 a similar name as the table and the fields have a 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.

C#
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.

C#
[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).

C#
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)



Comments and Discussions

 
QuestionHave a Five Pin
David Roh19-Mar-13 8:37
David Roh19-Mar-13 8:37 
GeneralMy vote of 1 Pin
Daniel Nutu15-Sep-10 0:12
Daniel Nutu15-Sep-10 0:12 
GeneralMy vote of 2 Pin
Andries Olivier13-Sep-10 21:16
professionalAndries Olivier13-Sep-10 21:16 
Not much information provided such as relationships, mappings, constraints, pk's etc. Just too simple.

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

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