1

Do I have to manually issue the Update-Database command in the package manager console even though I have automatic migrations turned on?

I'm running MVC4 and EF6. The solution is targeted for .NET 4 (I can't change this unfortunately).

EDIT: I'm aware of the difference between Code Migrations that would require me to properly seed existing tables. This question is more based towards a scenario where I add a table that has no relations, which I would think would just run?

EDIT 2: Table that should be added automatically

============

New Table definition

public class InboundFile : Entity
{
    [Required]
    public String FilePath { get; set; }
}

EF Config file

    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
        AutomaticMigrationDataLossAllowed = true;
        MigrationsDirectory = @"Migrations";
    }

    protected override void Seed(Unifi.Context.DBContext context)
    {
        context.InboundFiles.AddOrUpdate(
            x => x.FilePath,
            new InboundFile { CreateDate = DateTime.Now, ModifiedDate = DateTime.Now, FilePath = "c:/test", IsDeleted = false }
        );
    }

DB Context

public class DBContext : DbContext {

    public DBContext()
        : base("MyConn")
    {

    }

    public DbSet<User> Users { get; set; }

    public DbSet<UserRole> UserRoles { get; set; }

    public DbSet<Region> Regions { get; set; }

    public DbSet<InboundFile> InboundFiles { get; set; }

}
mituw16
  • 5,126
  • 3
  • 23
  • 48

2 Answers2

2

Try placing this in your Application_Start

Database.SetInitializer(new MigrateDatabaseToLatestVersion<DbContext, Configuration>);
using (var temp = new DbContext()) {
    temp.Database.Initialize(true);
}

This will force it to run update-database, and in doing so seed, every time your application starts.

Ben Black
  • 3,751
  • 2
  • 25
  • 43
0

Update-Database triggers an "update" operation. You can run your app and database will be updated on app start.

I prefer to run Update-Database manually because I can see if there are any db migration errors.

Andrei
  • 42,814
  • 35
  • 154
  • 218
  • Right, So that is where I'm getting a little confused. I added another table to my context, no relations so nothing to worry about error wise, then ran the application. What's weird to me is that the table was not added to my database even though I have auto-migrations turned on. What gets weirder is that I then manually issued `Update-Database` and it found and ran an automatic migration. – mituw16 Apr 04 '14 at 12:39
  • I'm not sure if it matters or not, but I have the web and context in different projects as part of the solution whole. – mituw16 Apr 04 '14 at 12:40
  • Could you please share new code for your table. I'd like to see changes in db context and entity class itself. – Andrei Apr 04 '14 at 12:42
  • Where is DbContext itself? – Andrei Apr 04 '14 at 13:10
  • Didn't think that was relevant, but I added it. – mituw16 Apr 04 '14 at 13:14