1

I'm wondering how I can use Entity Framework's Code First Migrations without using NuGet at all (so no commands via the package manager console).

I tried the following:

Database-Context

public sealed class MyContext : DbContext
{
  private const string ConnectionStringName = "MyDatabase";

  public MyContext()
    : base(ConnectionStringName)
  {}

  protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
    SetupMyModel(modelBuilder);
  }
}

Migration-Configuration

public class MyMigrationConfiguration : DbMigrationsConfiguration<MyContext>
{
  public MyMigrationConfiguration()
  {
    AutomaticMigrationsEnabled = false;
  }
}

Database-Initializer

System.Data.Entity.Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, MyMigrationConfiguration>());

InitialDatabaseCreation Migration

public class InitialDatabaseCreation : DbMigration, IMigrationMetadata
{
  public override void Up()
  {
    CreateTable("dbo.MyModel",
      c => new
      {
        Id = c.Guid(false, true),
        SomeProperty = c.Int(false)
      })
      .PrimaryKey(x => x.Id);
  }

  public override void Down()
  {
    DropTable("dbo.MyModel");
  }

  public string Id
  {
    get { return "0001_InitialDatabaseCreation"; }
  }

  public string Source
  {
    get { return null; }
  }

  public string Target
  {
    get { return Id; }
  }
}

As you can see, I wrote a context, a migration configuration and the migration itself. For the Migration, I'm not sure, if I implemented the IMigrationMetadata correcly. For the Target I just use the id, because I don't want to have any automatic migrations or the ability to use the package manager console. I think, this should be fine here?

I set a breakpoint at the Up method and debugged it, but it does not stop there, which means it doesn't get executed.

Therefor I want to know how to use the EF Code First migrations when writing everything manually.

4
  • I don't understand why you're trying to avoid the Nuget console - Nuget's here, it's a reality, you'll struggle to get much done whilst avoiding it both now and in the future... Commented Feb 24, 2014 at 11:09
  • Well, it's just something I want to try and didn't achieve it yet :) Commented Feb 24, 2014 at 12:22
  • @Damien_The_Unbeliever Another point is (I forgot): The model is madeup with interfaces only and it seems, that the build into auto model generation does not work as expected. Commented Feb 24, 2014 at 12:36
  • Try this Commented Jan 11, 2016 at 8:27

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.