Entity Framework - Code First Model Validation - entity-framework

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.

Related

EF Core Migrations not being recognized

after I execute the add-migration in the PMC the migration class is created with all the correct changes need in the Up method.
When I run the update-database command in the PMC I get the error
System.InvalidOperationException: The migration '***MigrationName***' was not found.
I have never seen this before and the file it says is not there clearly exist.
If I execute the remove-migrations command the newly created migration class is not removed, I get an error stating that the previous migration has already been applied.
It appears that all new migrations created with add-migrations are not recognized via the update-database or remove-migration commands
One of the reasons for this is that dbcontext is not in startup project and it is inside another project. In this case PM may not be able to find routes to complete migrations. We can use dotnet with explicit dbcontext and proj name where dbcontext is:
dotnet ef database update --context YourAppContext --project YourProj
We just had the same issue. The problem was that, we excluded the Migrations folder from the solution in Visual Studio.
Project ⇒ Show all files
Right Click on "Migrations"
» Include in project
The above steps resolved it for us.

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

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();

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.

How to cope with "No data stores are configured"?

I am currently playing with beta4 of EF7 using the blank ASP.NET web project template.
After having kicked off the existing migration, resulting in the tables being created in the localdb, the following occurs:
Strangely, when I clean up the migration-folder, including removing ApplicationDbContextModelSnapshot.cs and I run
dnx . ef migration add twice, I get the following error:
dnx : System.InvalidOperationException: No data stores are configured. Configure a data store by overriding OnConfiguring in your DbContext class or in the AddDbContext method when setting up services.
The second migration is not created. When I review the created migration it contains all tables whereas the database is already provisioned, so you should expect the migration being empty.
Then, when I remove the first migration and run the add migration command again more than once, all the migrations are correctly created, i.e. as empty files.
Can someone explain this to me? Is this expected behavior or is this a bug in beta4?
Tip for people coming from former EF-versions:
* Don't use the K command framework anymore.
* Don't use the Add-Migration cmdlets anymore.
Both have been replaced by dnx . (dot). (dnx = .NET execution environment)
Some references:
https://github.com/aspnet/EntityFramework/wiki/Entity-Framework-Design-Meeting-Notes---September-11,-2014
http://jameschambers.com/2015/05/project-k-dnvm-dnx-dnu-and-entity-framework-7-for-bonus-points/
Remove the constructor of ApplicationContext. It is a temporary workaround to enable deployment, but it interferes with the Migrations commands.

EF Add-Migration indicates "No pending explicit migrations" but Update-Database complains "..there are pending changes"

I frequently encounter the following situation with Entity Framework code first migrations. I add some migrations and update the database with them successfully. When later I wish to add a new migration, the Add-Migration command complains that its "Unable to generate an explicit migration because the following explicit migrations are pending...". I.e., it seems to forget that its already applied those migrations previously and therefore they shouldn't be now considered as pending. If I try to workaround this issue, Update-Database indicates "Unable to update database to match the current model because there are [new] pending changes...". I don't understand why EF migrations getting out of sync since I'm only making changes to the model.
For me the issue was that we had renamed the namespace of the migration 2014123456_Initial.cs.
But VS had not regenerated the namespace in its associated 2014123456_Initial.Designer.cs.
Once the Designer.cs was changed to use the same namespace, it all started working again.
I was having the same issue and the solution was:
Clean solution;
Build the project which has migrations enabled (just to make sure);
Build the Solution again;
This is pretty much equivalent of using the Rebuild Solution option in the Build menu.
I had the exact same problem and could not resolve it.
To add to IamDOM solution ALSO add an empty migration and update database.
For example:
Add-Migration Test
Update-Database
It worked for me few times now.
I found that VS had got confused somehow and not actually added the migrations to the .csproj. Closing, re-opening VS then re-including the migration in the project and saving-all worked.