Skip to content

Basic commands for Entity Framework – Code First

August 27, 2012

When using EF – Code First approach in a .NET project, usually you have to follow these steps.

1. Add the EntityFramework NuGet package.

Project –> Manage NuGet Packages…
Note: If you don’t have the Manage NuGet Packages… option you should install the latest version of NuGet
Select the Online tab
Select the EntityFramework package
Click Install

2. Create derived context.

public class BloggingContext : DbContext
public DbSet Blogs { get; set; }
public DbSet Posts { get; set; }

3. Create Models

4. Create a connection string in the App.Config file with the same name as your context.

Dealing with Model Changes

1. Enable Code First Migrations for our BloggingContext.

  • Tools -> Library Package Manager -> Package Manager Console
  • Run the Enable-Migrations command in Package Manager Console

2. Make necessary changes to the model

3. Run the Add-Migration AddUrl command in Package Manager Console.

4. Run the Update-Database command in Package Manager Console.


Ps: This post is mostly a sum-up from here


From → .NET

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: