Step 1: Create a New Empty ASP.NET MVC4 Application

We’ll start by creating a new ASP.NET MVC 4 Project within Visual Studio 2010.  Choose File->New Project and use Visual C# and the “Internet Application. This will create the Account and Home Controller Classes in the Project.

 Step 2: Create our Model

When using a code-first development workflow, we do not need to begin our application by creating a database.

Add a Donor.cs class in Models

Add the following Code in the Donor.


using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace BloodDonors.Models
    public class Donor
        [HiddenInput(DisplayValue = false)]
        public int DonorID { get; set; }
        public string Name { get; set; }
        public string bGroup { get; set; }
        public string Description { get; set; }
        public string Phone { get; set; }
        public string Website { get; set; }
        public string Email { get; set; }
        public string Address { get; set; }
        public string Country { get; set; }
        public double Latitude { get; set; }
        public double Longitude { get; set; }





Create the Database Context

The main class that coordinates Entity Framework functionality for a given data model is the database context class. You create this class by deriving from the System.Data.Entity.DbContext class. In your code you specify which entities are included in the data model. You can also customize certain Entity Framework behaviour. In the code for this project, the class is named DonorContext.

Create a DAL folder. In that folder create a new class file named DonorContext.cs, and replace the existing code with the following code:

using System;

using System.Collections.Generic;

using System.Data.Entity;

using BloodDonors.Models;

using System.Data.Entity.ModelConfiguration.Conventions;


namespace BloodDonors.Models


   public class SchoolContext : DbContext


       public DbSet<Donors> Donors { get; set; }


       protected override void OnModelCreating(DbModelBuilder modelBuilder)









This code creates a DbSet property for each entity set. In Entity Framework terminology, an entity set typically corresponds to a database table, and an entity corresponds to a row in the table. The statement in the OnModelCreating method prevents table names from being pluralized

Setting the Connection String

You don't have to create a connection string. If you don't create one, the Entity Framework will automatically create a SQL Server Express database.

Open the project Web.config file and add a new connection string to the connectionStrings collection, as shown in the following example. (Make sure you update the Web.config file in the root project folder. There's also a Web.config file is in the Views subfolder that you don't need to update. )

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

Initializing the Database with Test Data

In the DAL folder, create a new class file named DonorInitializer.cs and replace the existing code with the following code, which causes a database to be created when needed and loads test data into the new database



Last edited Jun 18, 2012 at 9:34 AM by mobin_ksa, version 4


No comments yet.