Over a million developers have joined DZone.

Using WebMatrix and the Entity Framework 4 Together

DZone's Guide to

Using WebMatrix and the Entity Framework 4 Together

Free Resource
Microsoft's new WebMatrix development tool has just hit version 1.0 and some .NET developers have been wondering if they will be able to use the Entity Framework 4 with it and include, for example, a Microsoft.Data.Entity.CTP.dll reference to a WebMatrix site.  This would allow devs to use something like code-first development in a WebMatrix project.  The following is a short tutorial on how to make this possible.

Start out by adding Microsoft.Data.Entity.CTP.dll to the ~/bin.  If you don't already have a database, create one now.  Once you have a database created, add a web.config file to the project and then add this code:
<?xml version="1.0"?>


<add name="DataContext" connectionString="Data Source=|DataDirectory|EF4.sdf" providerName="System.Data.SqlServerCe.4.0" />

<compilation debug="false" targetFramework="4.0" />

Be aware that the database may require a name change. 

Next, you'll need to create a model.  This example has a new cs file generated in app_code.  This model is named "GuestbookEntry.cs":
using System;
using System.ComponentModel.DataAnnotations;

public class GuestbookEntry
public int Id { get; set; }

public string Name { get; set; }

public string Email { get; set; }

public string Message { get; set; }

public DateTime Posted { get; set; }
This is the model that EF4 will use.

Now we can create a DataContext.  Create your DataContext.cs file and add this:
using System.Data.Entity;

public class DataContext : DbContext
public DbSet<GuestbookEntry> Guestbook { get; set; }
If you create a repository like the one shown below (GuestbookRepository.cs), you can work with your data much more easily:
using System;
using System.Collections.Generic;
using System.Linq;

public class GuestbookRepository
private DataContext _ctx = new DataContext();

public GuestbookEntry First(Func<GuestbookEntry, bool> where)
return _ctx.Guestbook.First(where);

public List<GuestbookEntry> GetAll()
return _ctx.Guestbook.ToList();

public void Add(GuestbookEntry entity)
entity.Posted = DateTime.Now;


public void Delete(GuestbookEntry entity)
The last step you must take is to confirm that the database is recreated after the model is changed.  In this example, we create a _start.cshtml that executes every time the site is started—similar to Application_Start in global.asax).  Take a look:
@using db = System.Data.Entity.Infrastructure;

db.Database.SetInitializer<DataContext>(new db.RecreateDatabaseIfModelChanges<DataContext>());
Finished!  All that's left to do is test this by adding the following code to default.cshtml:
var gb = new GuestbookRepository();

if (IsPost)
var entry = new GuestbookEntry()
Name = "Mikael",
Email = "my@email.com",
Message = "New message!"


var grid = new WebGrid(gb.GetAll());

<!DOCTYPE html>

<form action="/" method="post">
<input type="submit" value="Add post" />
When you click on the button, a new post is created and displayed using the WebGrid helper.

You can read more about this technique in the WebMatrix forums.  For a full array of WebMatrix tutorial resources, visit Microsoft's web development site.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}