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
{
...
}
Have you tried using the '-Force' parameter in the package manager console?
E.g.
Update-Database -Force -Verbose
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With