Code-First EntityFramework – step by step

This is first post for code-first entity framework and how to use it.

1. add entity framework, open project, in VS Package Management
PM> Install-Package EntityFramework

2. Don’t enable migrations, if you enable migrations then, add
Enable migrations only after database is created.

3. create class inherits from DbContext

1
2
3
4
5
    public class SchoolContext : DbContext {
        public DbSet<Course> Courses { get; set; }
        public DbSet<Department> Departments { get; set; }
 
    }

add connection string in web.config with same name ‘SchoolContext’

4. enable migrations
check screen dump, it will create ‘Migrations’ folder and configuration.cs
enable-migration

5. Add-Migration
PM> Add-Migration

VS will ask you to input name and generate cs with today’s Date, check screen dump above.

This entry was posted in entityframework. Bookmark the permalink.