How to enable migrations through code only without using Package manager console - entity-framework

I am using Entity Framework Code first in my application. But while using this after updating model, every time I have to enable automatic migrations and run update-database using package manager console. Does anyone has solution on this, whether we can automate this and update-database without using package manager console.

You only have to run Enable-Migrations once, to generate the Configuration.cs class and the Initial migration. After that, whenever you change the model you have to generate a new migration by running Add-Migration, and also you have to run Update-Database to apply the migration to your database, but you shouldn't need to run Enable-Migrations again.
If you want to automate the process you can enable the automatic generation of migrations by setting AutomaticMigrationsEnabled = true in the constructor of your Configuration class, and use the MigrateDatabaseToLatestVersion database initializer in your DbContext. This would allow you to just change your model code and not having to do anything with Package Manager Console, no Add-Migration nor Update-Database required. You still need to run Enable-Migrations once, at the beginning, after you create the project, to generate the Initial migration.
Anyway automatic migrations can cause some problems, I wouldn't really recommend you to use them. So I would keep on doing the manual Add-Migration commands whenever your model changes. You can still skip doing the Update-Database if you use the MigrateDatabaseToLatestVersion database initializer.
Another way to do it would be using a DbMigrator to run the equivalent to Update-Database from your code (but you don't need to do this if you are using the MigrateDatabaseToLatestVersion database initializer):
var migrator = new DbMigrator(new DbMigrationsConfiguration());
migrator.Update();

Related

Entity Framework Migrations: Pending migration after "update-database"

I'm using entity framework version 6.1.3 and .NET Framework 4.0.
I'm trying to use Migrations from Code-First from existing Database.
I'm having a trouble that whenever I use the Update-Database it makes the changes from my migration, and changes my database, but it keeps the migration as "Pending" and when I try to add another migration with Add-Migration, it says I have pending migration.
Example of the problem:
Run the Enable-Migrations command from the Package Manager Console.
Run the Add-Migration first command to create an initial migration.
Remove all the code in Up() method for the initial migration, because I don't want to create the tables that already exists.
Run the Update-Database command to apply the initial migration to my database (it runs successfully).
Make changes to my code-first model (add column to existing table).
Run the Add-Migration second command to create a new migration.
When I run step 6, I get the following error:
Unable to generate an explicit migration because the following explicit migrations are pending: [201601111013546_first]. Apply the pending explicit migrations before attempting to generate a new explicit migration.
If I delete the migration "[201601111013546_first]" from my Migrations folder, I can run step 6, and use the Update-Database, and it successfully changes my database, adding the column I wanted, but if I change my model again and use Add-Migration third, it says the same error, that the second migration is still pending.
Also, if instead of steps 5 and 6, I just run Update-Database again, it runs the same script returning the obvious error of already having a 'MigrationId' in the table dbo."__MigrationHistory":
ERROR: 23505: duplicate key value violates unique constraint "PK_dbo.__MigrationHistory"
I already tried:
Create a new project (ConsoleApplication) and made the same steps getting the same errors.
Delete entire Migration folder and run Enable-Migrations again.
Verify the namespace of the Migration and it's .Designer file.
I have only one connectionString but still tried the commands with -ConnectionStringName.
Any suggestions?
Thanks.

Entity Framework - Code First Model Validation

In my project, I'm using Entity Framework 6.1.1 with Code First Migrations.
When I make a change in the model, I can sync my database with these commands in the Package Manager Console:
PM> Add-Migration "MIGRATION-NAME"
PM> Update-Database
Right now I am making several changes in my model, is there any command I can run to validate my model?
I believe you're referring to validate option that we get in Model First approach. For code first, there is no separate validate command, if Add-Migration fails that gives you the validation error. As this command is executed in transaction, all the partial changes by this command will be rolled-back in case of any error.

How do you use entity framework migrations for new and existing databases?

Let's say I have an existing database and I use EF migrations to get its initial state.
Enable-Migrations –EnableAutomaticMigrations
add-migration Initial
If I go ahead and run update-database, I get an error that my objects already exist. The solution I'm told is to remove the code in the Up() and Down() methods. That works fine. Now I can modify my models and add new migrations and then update to them accordingly.
But what do I do when I want to create a new database? I went ahead and restored the code in the Up/Down methods, and that worked for creating a new DB, but if I tried applying it to an existing database I'm back to the objects already exist. I'm guessing I need to be able to initialize the history table on the existing database.
I found a sortof workaround which will probably do the trick.
Make sure you delete the _MigrationHistory table on the existing database. Run the following in the PMC
Enable-Migrations –EnableAutomaticMigrations -Force
add-migration Initial
Update-Database -Script
The last command will generate a new SQL script and open in your IDE. Remove the code in the Initial Up() Down() methods. You can create a new database, then execute this script on that database and continue to use migrations.
Hopefully I can find a more elegant way of doing this.

Update-Database tries to do an automatic migration even with automatic migrations disabled

I work on a team of 4 developers using EF5, everyone working on their own local database. Up until now we've been using automatic migrations but we're nearing the point where we need to release to production so we've disabled automatic migrations and started adding explicit code-based migrations.
Here is the problem: I ran the Update-Database command after a developer created a new explicit migration and I get the following error:
Applying code-based migrations: [201209080142319_CreatedDate.LastModifiedDate.Additions].
Applying code-based migration: 201209080142319_CreatedDate.LastModifiedDate.Additions.
Applying automatic migration: 201209080142319_CreatedDate.LastModifiedDate.Additions_AutomaticMigration.
Automatic migration was not applied because it would result in data loss.
Why do I get this error even though I've disabled automatic migrations? I can fix this error by deleting the explicit migration and then re-scaffolding it (running Add-Migration). Then Update-Database runs fine and doesn't mention anything about 'Automatic migration...' Also, the code in the migration created by me when I run Add-Migration is identical to the one created by my teammate. I don't see why it would even try to do an automatic migration since AutomaticMigrationsEnabled = false;.
What am I missing here?
I hate to answer my own question but I encountered this problem again. A developer on my team re-enabled automatic migrations on their local machine and then created an explicit migration, which reproduced this behavior as soon as I ran it.
Entity framework will always run an automatic migration before it runs an explicit migration that has the Source property defined in its .resx file, even if AutomaticMigrationsEnabled = false. An explicit migration will only have the Source property set if it is created after an automatic migration has been run.
The upshot is that disabling automatic migrations only means that EF won't automatically upgrade your schema when it detects model changes - but it might still do an automatic migration if it needs to fill in a gap between some explicit migrations. To avoid this behavior, don't use a mixture of automatic migrations and explicit migrations.
public class Configuration : DbMigrationsConfiguration<bailencasino.com.dal.Context.BlncnoContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
AutomaticMigrationDataLossAllowed = true;
}
Add AutomaticMigrationDataLossAllowed = true; so you assume that you want to allow EF to add remove SQL objects that result in data loss.
My team has experienced something that may be related to this. If two team members both add a migration, check in their code, get latest, and then perform update-database, the second one will get an error because there is a migration "skipped" - their system sees the team member's migration was never implemented.
We've started checking everything in and getting latest, doing the update-database (if a team member added a new migration), then doing add-migration, update-database, check-in.
AutomaticMigrationEnabled = false prevents your application from updating the database on its own.
But since your running Update-database yourself, Update-Database checks the current state of the database and then runs all the migration steps not already in the database including the changes in the model (dbContext) that do not have a code-based migration yet.
My guess is that there is a change in the model that would cause data loss.
You can use the -force parameter to apply the changes still when there is data loss.
#Dave Graves is right. I had the same issue and did his fix in order to disable the auto migration of particular migration file
heres some instructions:
go to where your migration files are located in solution explorer (if you are using visual studio)
look for the migration file causing that auto migration e.g: 202008181102535_BlogPost.cs
expand it and you will see its designer file. open that file
inside that file, you will see the Source property of it is being set by something like Resources.GetString("Source"). replace that with null and then try again to manually execute update-database in your package manager console.

No initial create with Entity Framework migrations

I'm trying to get Entity framework migrations working. I've enabled code first migrations, its created a migrations folder, config file and the mig history table, but no initial create. Am i missing a step? This is a new db created by EF (4.3.1).
This behavior is not in place by default, but it is available to you easily in many different forms.
You can call context.Database.CreateIfNotExists(); at application startup.
You can use one of the built-in DatabaseInitializers. The CreateDatabaseIfNotExists initializer is built into EntityFramework and just needs to be added to your project.
You could create your own custom database initializer which includes option #1 inside of itself. Example: Code First Migrations and initialization
You can include DatabaseInitializers in your project either by code or via a config file.
Include an EntityFramework Database Initializer via code:
In your application startup you can setup the DatabaseInitializer like so:
System.Data.Entity.Database.SetInitializer<DairyMmmContext>(new System.Data.Entity.CreateDatabaseIfNotExists<DairyMmmContext>());
NOTE: this code has changed multiple times throughout the life of entityframework! This example is for EF 4.3 which is the current production release available via nuget.
Include an EntityFramework Database Initializer via configuration element:
<configuration>
<entityFramework>
<contexts>
<context type="MyNamespace.MyEFDataContext, AssemblyName">
<databaseInitializer
type="System.Data.Entity.CreateDatabaseIfNotExists`2[[MyNamespace.MyEFDataContext, AssemblyName],
[MyNamespace.Migrations.Configuration, AssemblyName]], EntityFramework" />
</context>
</contexts>
</entityFramework>
</configuration>
You'll notice this can be a little "ungraceful" with this configuration. You need to replace AssemblyName above with the name of the assembly you keep your entityframework stuff in, replace MyNamespace.MyEFDataContext with the fully qualified name of your entityframework data context, and replace MyNamespace.Migrations.Configuration with the fully qualified name to your configuration class (by default in the Migration folder inside your project).
EDIT: Edited to respond to additional comments
A migration is a change from one schema definition to another schema definition. Creating the empty database is not a migration (but everything after that is). There will be no migration source file in your project for just creating an empty db, that is done in code by the initializer.
If you are already using the DropCreateDatabaseAlways initializer it should be doing that. However, I noticed you are setting the initializer in code which means there is the opportunity for a timing problem (setting the initializer after your context is already past the point of calling any initializers).
You can force entityframework to run your initializer at any point in code with context.Database.Initialize(true); (The parameter is a true/false to force the initialization regardless of the current state). That would drop and recreate your database every time.
But you can also just make sure your initializer is setup as early as possible in your application's life cycle (before you have created a single instance of your context).
"Initial Create" is NOT created automatically! You need to create that yourself. Some tutorials of EF are confusing and I had the same misunderstanding as you.
What you need to do:
-Add-Migration InitialModel
If you already have created your database tables and domain model, then:
-Add-Migration InitialModel -IgnoreChanges
From this point, your code will be in sync with database. Anytime you change the code, you can use Add-Migration to add the changes to your database.
The article / tutorial here here (on microsoft.com)
describes the reason that an initialCreate migration doesn't exist. The migration will only be added if the Database already exists. Otherwise, the first migration will be the 'initialCreate' as there is no point in creating a migration to a Database that doesn't exist yet... no DB means there is nothing to roll back to, on a down migration.
Here is the pertinent paragraph:
Run the Enable-Migrations command in Package Manager Console
This command has added a Migrations folder to our project, this new folder contains two files:
The Configuration class. This class allows you to configure how Migrations behaves for your context. For this walkthrough we will just use the default configuration.
Because there is just a single Code First context in your project, Enable-Migrations has automatically filled in the context type this configuration applies to.
An InitialCreate migration. This migration was generated because we already had Code First create a database for us, before we enabled migrations. The code in this scaffolded migration represents the objects that have already been created in the database. In our case that is the Blog table with a BlogId and Name columns. The filename includes a timestamp to help with ordering.
If the database had not already been created this InitialCreate migration would not have been added to the project. Instead, the first time we call Add-Migration the code to create these tables would be scaffolded to a new migration.
Not sure it's the same but I had a similar issue. I think my problem was related to the fact that I dont use a connections string from the config file to get my connection string.
Fiddling with the start up project in the solution and also the projet combo in the Package Manager Console I was able to generate that first migration.
Also make shure you have a connections string with the name of your dbContext class so the Package Manager can find it.
I am using EF 6 RC1 and ran into this problem where neither the InitialCreate nor __MigrationHistory were being created when running Enable-Migrations.
Actually, just after upgrading from EF 5 to EF 6 I ran Enable-Migrations and for some reason it created a __MigrationHistory table using the EF 5 schema, so I deleted it and my Migrations directory and tried to start over.
But every time I deleted the Migrations directory it wouldn't create an InitialCreate or __MigrationHistory. I tried dropping and recreating the database and restarting Visual Studio 2012 to no avail. I gave up for the day and the next morning tried again - after letting my computer sit for about 8 hours it then created the InitialCreate. I am guessing there must be a cache somewhere that has a really long timeout - anyone? I am also guessing that rebooting might clear the cache, but I didn't try that.
Whatever the case, it is possible to use PM> Add-Migration InitialCreate to do that step manually.
Anyway, I still didn't get a __MigrationHistory table. Apparently, EF 6 has changed from creating it during the Enable-Migrations command to instead only creating it during the Update-Database command. And since my schema had already been created at that point, I needed to tear it down and recreate it manually:
PM> Update-Database -TargetMigration:0
PM> Update-Database
I also stopped after the first command to check the state of the database to ensure I was updating the correct one, since according to this, the database connection string is picked up or autogenerated depending on the configuration, and unless it is configured right, there is no guarantee you are going to access the database or instance of SQL Server you intend to.
After running both commands it created a __MigrationHistory table - and it didn't create it as a system table (which I didn't really want anyway), so all is good. Not exactly the same problem as the OP, but hopefully this will be helpful to someone else.
References:
http://elegantcode.com/2012/04/12/entity-framework-migrations-tips/
Reset Entity-Framework Migrations
How to recreate migrations from scratch
I know this is old, but there is no accepted answer and I had same issue.
The trick is Enable-Migrations command. As stated here there is a command Enable-Migrations –EnableAutomaticMigrations. What it does it starts migrations exactly where you are.
If you want the first migration to be creation of database, just run Enable-Migrations (without --EnableAutomaticMigrations).
And remember to set initializer:
Database.SetInitializer(new MigrateDatabaseToLatestVersion<LicenseContext, Configuration>());