HighwayFramework / Highway.Data

The fastest and smoothest way to great architecture

Home Page:http://hwyfwk.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

EF Migrations Should Just Work

trayburn opened this issue · comments

I am having an issue with EF Migrations and Highway.Data. Is there any documentation on how to configure this?

I tried to create my own SqlCompactContext. The issue I ran into there was when I tried to update the local database to the latest migration, the migration didn't seem to run. I was using the following to upgrade to the latest migration:

Database.SetInitializer(new MigrateDatabaseToLatestVersion<SqlCompactContext, Configuration>());

Edit:
A more specific question would be, do you set the Configuration file of the Migrations to inherit from DbMigrationsConfiguration<Highway.Data.DataContext> or a custom data context like SqlCompactContext?

If it's the Highway.Data.DataContext, how do you provide the implementation of IDbContextFactory for the Migrations to work?

Check out This git hub repo – Develop Branch

https://github.com/DevlinLiles/MigrationExample

From: bluefindata [mailto:notifications@github.com]
Sent: Friday, February 21, 2014 3:08 PM
To: HighwayFramework/Highway.Data
Subject: Re: [Highway.Data] EF Migrations Should Just Work (#43)

I am having an issue with EF Migrations and Highway.Data. Is there any documentation on how to configure this?

When I set the configuration class to the Highway.Data DataContext, I get the error "The target context 'Highway.Data.DataContext' is not constructible. Add a default constructor or provide an implementation of IDbContextFactory."

Since I was running into the issue above, I tried to create my own SqlCompactContext. The issue I ran into there was when I tried to update the local database to the latest migration, the migration didn't seem to run. I was using the following to upgrade to the latest migration:

Database.SetInitializer(new MigrateDatabaseToLatestVersion());


Reply to this email directly or view it on GitHub #43 (comment) .

To sum up Devlin's code example, you need to have a class inherit from Highway.Data.DataContext that has a parameterless constructor:

public class MigrationContext : DataContext
{
    public MigrationContext() 
        : base("Data Source=.;Initial Catalog=MigrationExample;Integrated Security=True", 
               new ExampleMappings()) { }
}

Then in your EF migration's Configuration.cs file make sure to point it at your new DataContext:

internal sealed class Configuration : DbMigrationsConfiguration<MigrationContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
    }

    protected override void Seed(MigrationContext context) { }
}

Hey guys, thank you for your comments last year.

I have looked back over your solutions and I have it implemented just like you explained, but I am running into another issue. I am using EF 6, so I'm not sure if something has changed in EF that is causing the issue or not. As far as I recall, your solution worked for me last year, so I am not quite sure what would have changed if it did.

I was not able to find where you were setting the initialize in your solutions, but this is how I am setting it when the application starts:

Database.SetInitializer(new MigrateDatabaseToLatestVersion<SqlCompactContext, Configuration>());

When researching this problem, I came across a solution that suggested calling Database.Initialize() to force the initialization process, but this did not work for me. I called it by creating the AdvancedCommand below and calling it right after SetInitializer. Here is the link to that solution: http://stackoverflow.com/questions/15537185/migratedatabasetolatestversion-initializer-failing-to-create-database

public class InitializeDatabase : AdvancedCommand
{
public InitializeDatabase()
{
ContextQuery = c => c.Database.Initialize(true);
}
}

Here are my context files:

Configuration:
public class Configuration : DbMigrationsConfiguration
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
}

    protected override void Seed(SqlCompactContext context)
    {        }
}

Db Context:
public class SqlCompactContext : DataContext
{
public SqlCompactContext()
: base(@"Data Source=|DataDirectory|\MyLocalDb.sdf; Password=*******;Initial Catalog=MyLocalDb;Integrated Security=True", new MyMappings())
{

    }
}

Mappings:
public class MyMappings : IMappingConfiguration
{
public void ConfigureModelBuilder(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.AddFromAssembly(Assembly.GetExecutingAssembly());

        modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
    }
}

I'm still not sure why my original solution would not work, but I forced the update by implementing it manually.

var cfg = new Configuration();
cfg.TargetDatabase =
new DbConnectionInfo(
theConnectionString,
"provider" );

DbMigrator dbMigrator = new DbMigrator( cfg );
dbMigrator.Update();

Here's the link: http://stackoverflow.com/questions/17901215/entity-framework-code-first-migrations-production-wpf-application