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