Code exerpts follow:
Automatically generates the database schema when class is created:
namespace SproutCoreMVC.Models
{
public class Task
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int id { get; set; }
public string description { get; set; }
public int order { get; set; }
public bool isDone { get; set; }
public string guid { get {return "/tasks/" + id;} }
}
public class SCModels : DbContext
{
public DbSet<Task> Tasks { get; set; }
}
public class SCModelsInitializer : DropCreateDatabaseIfModelChanges<SCModels>
{
protected override void Seed(SCModels context)
{
var tasks = new List<Task>
{
new Task {
order = 1,
isDone = false,
description = "Some Description"
}
};
tasks.ForEach(d => context.Tasks.Add(d));
context.SaveChanges();
}
}
}This is the REST implementation. Easy da?
namespace SproutCoreMVC.Controllers
{
public class TasksController : Controller
{
SCModels db = new SCModels();
// GET: /tasks
[HttpGet]
[NoCache]
public ActionResult List()
{
return Json(new {content = db.Tasks}, JsonRequestBehavior.AllowGet);
}
//GET: /tasks/id
[HttpGet]
[NoCache]
public JsonResult Get(int id)
{
return Json(db.Tasks.Find(id), JsonRequestBehavior.AllowGet);
}
//PUT: /tasks/id
[HttpPut]
public ActionResult Update(Task task)
{
var taskToUpdate = db.Tasks.Find(task.id);
UpdateModel(taskToUpdate);
db.SaveChanges();
return Json(taskToUpdate);
}
//POST: /tasks
[HttpPost]
public ActionResult Create(Task task)
{
db.Tasks.Add(task);
db.SaveChanges();
//set location and to redirect headers
Response.StatusCode = 201;
Response.RedirectLocation = task.guid;
return new EmptyResult();
}
//DELETE: /tasks/id
[HttpDelete]
public ActionResult Delete(int id)
{
db.Tasks.Remove(db.Tasks.Find(id));
db.SaveChanges();
return new EmptyResult();
}
}
} I left out the routing information. As you can see the integration between SproutCore and asp.net MVC3 and Entity Framework 4 is quite straightforward and painless. I actually only write about 70 lines of code. Is much easier than you half of project.



