Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutomaticMigrationDataLossAllowed not working

I am having a scenario where the AutomaticMigrationDataLossAllowed property of my Configuration class is not working in Entity Framework 6.

I set both the required properties to true, but yet I receive an update exception which states that potential data loss could occur. Ironically, it advices me to set the properties to true that I have already set to true.

Here's how I instantiate my model container (context).

Database.SetInitializer(new ModelInitializer());
Entities = new ModelContainer();

Here's the relevant part of my ModelInitializer class.

internal class ModelInitializer : IDatabaseInitializer<ModelContainer>
{

    private static bool _usedBefore;

    public void InitializeDatabase(ModelContainer context)
    {

        ...

        var migrateInitializer = new MigrateDatabaseToLatestVersion<ModelContainer, Configuration>();
        migrateInitializer.InitializeDatabase(context);
    }
}

And finally, here's my Configuration class.

internal sealed class Configuration : DbMigrationsConfiguration<ModelContainer>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
        AutomaticMigrationDataLossAllowed = true;
    }

    protected override void Seed(ModelContainer context)
    {

    }
}

My ModelContainer class (the context) basically just has a few properties and classes in it, so I don't think that's relevant to the problem. Here's the declaration though.

internal class ModelContainer : DbContext
{
    ...
}
like image 814
Mathias Lykkegaard Lorenzen Avatar asked Apr 18 '14 23:04

Mathias Lykkegaard Lorenzen


2 Answers

Have you tried using the '-Force' parameter in the package manager console?

E.g.

Update-Database -Force -Verbose
like image 100
Aydin Avatar answered Sep 18 '22 17:09

Aydin


Are you using separate library for data access??

if yes then you need to provide its name when running query:

Add-Migration -StartUpProjectName "Your DAL Project" MyNewMigration

Update-Database -StartUpProjectName "Your DAL Project" -Verbose

like image 43
Ajit Kumar Avatar answered Sep 17 '22 17:09

Ajit Kumar