DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
11 Monitoring and Observability Tools for 2023
Learn more
  1. DZone
  2. Coding
  3. Frameworks
  4. Using WebMatrix and the Entity Framework 4 Together

Using WebMatrix and the Entity Framework 4 Together

Mitch Pronschinske user avatar by
Mitch Pronschinske
·
Jan. 17, 11 · Interview
Like (0)
Save
Tweet
Share
17.02K Views

Join the DZone community and get the full member experience.

Join For Free
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"?>

<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>
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
{
[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; }
}
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;

_ctx.Guestbook.Add(entity);
_ctx.SaveChanges();
}

public void Delete(GuestbookEntry entity)
{
_ctx.Guestbook.Remove(entity);
_ctx.SaveChanges();
}
}
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!"
};

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>
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.
Entity Framework Database Framework

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Running Databases on Kubernetes
  • A Beginner's Guide to Infrastructure as Code
  • Real-Time Analytics for IoT
  • Container Security: Don't Let Your Guard Down

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: