Every time I want to update database with code-first migrations I need to open Package Manager Console, set up Default project to project where I have migrations and write command:
update-database -startupprojectname CoreConsole
How I can set up in my solution:
1) Default project in dropdown where migrations are stored
2) startupprojectname where config with connection settings is stored
The target is to only write command update-database in console without any parameters and additional dropdown selections.
Related
I am trying to use Migrations in my project but when trying to update the database, only the base migration is applied.
I have a total of two migrations.
The Base migration(initialy created)
CreateNewProperty(Change includes adding a new property to one of the tables)
I see Enable-Migration mentioned in the Microsoft Documentation but it seems to have gone obsolete.
In addition to this, I also a mention of using the following in the Context class to enable latest migrations, but SetInitialier() method is no longer available:
Database.SetInitializer(new MigrateDatabaseToLatestVersion<DatabaseContext, EF6Console.Migrations.Configuration>());
When I try to remove all migrations and bring it to the initial state by executing:
update-databse 0
it fails with the following error:
ALTER TABLE DROP COLUMN failed because column 'test' does not exist in
table 'Item'.
which is basically the update done in the CreateNewproperty migration and this error is expected as this migration was never applied. Why is EF trying to downgrade a migration, when it did not apply it in the first place.
Use below command to Migrate to a Specific Version (Including Downgrade)
update-database -targetmigration:MigrationName
More information visit
https://learn.microsoft.com/en-us/ef/ef6/modeling/code-first/migrations/#migrate-to-a-specific-version-including-downgrade
Is it possible that your snapshot has become messed up somehow? You could try deleting that and then create a new migration (one that you could throw away) to rebuild it. Then try running update-database again.
In order to get rid of writing Migration and Update commands each time your data model changes, you can simply write two command line files as EF CLI and just click them to execute Migration and Update commands. These command files create the Migration name using the date and time of your machine.
For the add migration file set a name such as 01-add_migration.cmd (with .cmd extension) with the following content:
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c_%%a_%%b)
For /f "tokens=1-2 delims=/:" %%a in ("%TIME: =0%") do (set mytime=%%a%%b)
dotnet ef --startup-project ../Product.Api migrations add V%mydate%_%mytime% -o Persistence/Migrations -c ApplicationDbContext
pause
and for the update database set e file name such as 02-update_db.cmd with the content as bellow:
dotnet ef --startup-project ../Product.Api database update -c ApplicationDbContext
pause
You can add other parameters of Migration and Update commands as well.
So in my opinion, if you have created lots of migrations and started changing up the classes during the creation of your app, you might have missed something, which happens to many developers.
If that is your case, you should try to delete the migrations history in the database and the tables that were created using the migrations.
The migrations history table is located in the tables folder of the database and looks like this:
Then delete the migrations folder that you have in your Project.
Search for it in the solution explorer.
It should look something like this:
Next, do what you would do from the beginning.
Add-Migration [Name]
Update-Database
Okay, so this is what worked:
I deleted the database completely and then ran the update-databse command.
On running this, first the base migration was applied and then the second migration. And now when I create a third migration and apply it is working as expected!
I'm tried to Add-Migration in my asp.net core application but migration not working
Please any one give me ans for this what's the issue
Please check this image i added all information reading issue everything i combined into single screen-shot for more clarification
you need to provide a name for the migration just like this Add-Migration V1, after creating the migration you need to run the Update-Database to apply the migration. BTW if there is any build error nothing will work. you need to ensure there is not build error.
I would do this:
1) Remove the Migrations folder in your project
2) Delete the _MigrationHistory table from your database.
3) Run the Enable-Migrations command from your Package Manager Console.
4) Run the Add-Migration Init command to create a migration.
5) Remove all of the code lines in Up() function for the initial migration. (To create an empty migration.)
6) Run the Update-Database command to apply your empty migration.
7) Make changes to your code first model for preparing to adding your real migration.
8) Run the Add-Migration InitNew command to create a new migration.
9)Run the Update-Database command to apply the changes.
Taken from: https://stackoverflow.com/a/44019743/6221069
I've published my ASP application on my local machin using IIS10.
when I try to login or register a new user after deployment I got this error
Unable to update database to match the current model because there are >pending changes and automatic migration is disabled. Either write the pending >model changes to a code-based migration or enable automatic migration. Set >DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable >automatic migration.
I use visual studio 2013, Entityframework 6.1.3,Windows Authentication
You need to run Update-Database against your target database.
Open the package manager console window and select your Entity Framework project, then type that command. As long as the config is pointing to the correct DB, it should update it.
PM> Update-Database
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
No pending explicit migrations.
Running Seed method.
PM>
Ok, so I've read the other threads and I'm still not getting anywhere. I have my project set as Startup, I have a connection string in the App.config. I'm using LocalDb in VS2013.
If I delete my database, it creates it ok, BUT, if I try
PM> Enable-Migrations
it tells me that Migrations have already been enabled in project TestCodeFirst. If I then try
PM> Update-Database
it tells me No migrations configuration type was found in the assembly 'TestCodeFirst', and suggests that I try to Enable-Migrations. I have tried adding the -Force parameter. The Migrations folder has only Configuration.cs, and in that file, I've tried setting AutomaticMigrationsEnabled both true and false, with no difference. I'm not lost, cause I can always delete my DB and rerun, but I don't see any way to make the migrations feature work as advertised. Confession: At one point, I took PM's suggestion and tried deleting the Migrations folder. That may have been a mistake.
I have this code snippet in my Program.cs file (I'm just testing this in a console app):
class Program
{
static void Main(string[] args)
{
using (TestContext db = new TestContext())
{
...
Db.ThisOrThat.Add(stuff);
Db.SaveChanges();
...
}
}
}
The Entity Framework Code First approach creates your database automatically, a table will be automatically added to the database by the Code First that is helpful to record the schema of database synchronization with the related model class. Entity Framework throws an error, the database is not synchronized with the class.
To perform the Code First Migration in the database:
Delete the MDF file of the database
Open Package Manager Console and write the following command
Enable-Migrations -ContextTypeName ConsoleApp.Models.TestContext
It creates the Configurations.csclass, then you can edit your code
After Building the application enter the following command in the Package Manager Console:
add-migration Initial
update-database
Note: If you receive an error then you need to ensure that the mdf file is deleted permanently from the App_Data folder. Otherwise you can go to the SQL Server Object Explorer, expand the localDb server and in the Database option ensure that the mdf file is deleted.
Debug the appliction
You'll need to run the "Add-Migration" command that will actually add the migration that the 'Update-Database' command will run:
Code First Migrations
"Add-Migration will scaffold the next migration based on changes you have made to your model since the last migration was created"
I removed the data annotation Required from a field. Do I need to use migrations to apply that in the DB? If yes, how can I do that?
To add a migration you go to the Package Manager Console
Run command -> Enable-Migrations
Run command -> add-migration [migration_name]
Run command -> update-database
To apply on production database you can create a script in the following manner
Update-Database -Script -SourceMigration:[NameOfMigrationYourDatabaseIsRunning]
run
update-database -verbose
in the package manager console