install-package EntityFramework
or
update-package EntityFramework
Install EF Power Tools to visualize code first
or
update-package EntityFramework
Install EF Power Tools to visualize code first
Context class
namespace EF6Tutorial {
public class BooksContext:DbContext {
public BooksContext()
: base("BooksConnection") {
}
public DbSet Titles {get;set;}
public DbSet Genres {get;set;}
public DbSet Authors {
get;
set;
}
public DbSet TitleMetadatas {
get;
set;
}
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
modelBuilder
.Entity()
.Property(t => t.AuthorName).HasMaxLength(500).HasColumnAnnotation( IndexAnnotation.AnnotationName,
new IndexAnnotation(
new IndexAttribute("IX_AuthorName", 1)));
base.OnModelCreating(modelBuilder);
}
public static void Initialise(){
Database.SetInitializer(
new MigrateDatabaseToLatestVersion()
);
}
}
================
PM> enable-migrations
internal class Configuration : DbMigrationsConfiguration
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed=true;
ContextKey = "EF6Tutorial.BooksContext";
}
protected override void Seed(EF6Tutorial.BooksContext context)
{
// This method will be called after migrating to the latest version.
// You can use the DbSet.AddOrUpdate() helper extension method
// to avoid creating duplicate seed data. E.g.
//
// context.People.AddOrUpdate(
// p => p.FullName,
// new Person { FullName = "Andrew Peters" },
// new Person { FullName = "Brice Lambson" },
// new Person { FullName = "Rowan Miller" }
// );
//
}
}