Page 8 - EntityFrameworkNotesForProfessionals-1-10
P. 8
Install-Package EntityFramework
This will install Entity Framework and automatically add a reference to the assembly in your project.
Section 1.2: Using Entity Framework from C# (Code First)
Code first allows you to create your entities (classes) without using a GUI designer or a .edmx file. It is named Code
first, because you can create your models first and Entity framework will create database according to mappings for
you automatically. Or you can also use this approach with existing database, which is called code first with existing
database For example, if you want a table to hold a list of planets:
public class Planet
{
public string Name { get; set; }
public decimal AverageDistanceFromSun { get; set; }
}
Now create your context which is the bridge between your entity classes and the database. Give it one or more
DbSet<> properties:
using System.Data.Entity;
public class PlanetContext : DbContext
{
public DbSet<Planet> Planets { get; set; }
}
We can use this by doing the following:
using(var context = new PlanetContext())
{
var jupiter = new Planet
{
GoalKicker.com – Entity Framework Notes for Professionals 4