To me it doesn't seem like these other answers are sufficient.
You can turn off the EF initializer:
public ApplicationContext : DbContext
{
public ApplicationContext()
: base("ConnectionStringName")
{
Database.SetInitializer<ApplicationContext>(null);
}
// DbSets here
public DbSet<Part> Parts {get; set;}
// override OnModelCreating below ...
}
And then use Fluent API / data annotations however you normally would to setup your POCOs/models to match the existing DB.
Details at this blog: http://cpratt.co/entity-framework-code-first-with-existing-database/
In case that URL does not work in the future - here's what I mean:
After having set the Initializer off above, configure your POCO's that correspond to a table:
public class Part
{
public string PartID {get; set;}
public string Description {get; set;}
public decimal Weightlbs {get; set;}
public decimal Price {get; set;}
}
Then map your POCO to the existing DB table by overriding this method in your Application Context
class:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Code First will assume a lot, but if you need to override things:
modelBuilder.Entity<Part>().ToTable("db_PartTable");
modelBuilder.Entity<Part>().Property(p => p.PartID)
.HasColumnName("Part_ID");
modelBuilder.Entity<Part>().Property(p => p.Description)
.HasMaxLength(100)
}
Another good blog for this, by Scott Guthrie: http://weblogs.asp.net/scottgu/using-ef-code-first-with-an-existing-database