Using WebMatrix and the Entity Framework 4 Together
Join the DZone community and get the full member experience.
Join For FreeMicrosoft'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:
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":
Now we can create a DataContext. Create your DataContext.cs file and add this:
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.
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"?>Be aware that the database may require a name change.
<configuration>
<connectionStrings>
<add name="DataContext" connectionString="Data Source=|DataDirectory|EF4.sdf" providerName="System.Data.SqlServerCe.4.0" />
</connectionStrings>
<system.web>
<compilation debug="false" targetFramework="4.0" />
</system.web>
</configuration>
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;This is the model that EF4 will use.
using System.ComponentModel.DataAnnotations;
public class GuestbookEntry
{
[ScaffoldColumn(false)]
public int Id { get; set; }
[Required]
[StringLength(30)]
public string Name { get; set; }
[Required]
[StringLength(60)]
public string Email { get; set; }
[Required]
[StringLength(1000)]
public string Message { get; set; }
[Timestamp]
public DateTime Posted { get; set; }
}
Now we can create a DataContext. Create your DataContext.cs file and add this:
using System.Data.Entity;If you create a repository like the one shown below (GuestbookRepository.cs), you can work with your data much more easily:
public class DataContext : DbContext
{
public DbSet<GuestbookEntry> Guestbook { get; set; }
}
using System;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 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;
_ctx.Guestbook.Add(entity);
_ctx.SaveChanges();
}
public void Delete(GuestbookEntry entity)
{
_ctx.Guestbook.Remove(entity);
_ctx.SaveChanges();
}
}
@using db = System.Data.Entity.Infrastructure;Finished! All that's left to do is test this by adding the following code to default.cshtml:
@{
db.Database.SetInitializer<DataContext>(new db.RecreateDatabaseIfModelChanges<DataContext>());
}
@{When you click on the button, a new post is created and displayed using the WebGrid helper.
var gb = new GuestbookRepository();
if (IsPost)
{
var entry = new GuestbookEntry()
{
Name = "Mikael",
Email = "my@email.com",
Message = "New message!"
};
gb.Add(entry);
}
var grid = new WebGrid(gb.GetAll());
}
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
@grid.GetHtml()
<form action="/" method="post">
<input type="submit" value="Add post" />
</form>
</body>
</html>
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.
Entity Framework
Database
Framework
Opinions expressed by DZone contributors are their own.
Comments