How to tell NuGet for past migration - c#

So I copy project solution form my friend and all migration where lost so if I change like username length to 129 and say to console Add-Migration it gives me empty migration?
So no UP and DOWN
So how do i need to do , to tell NuGet for past migration do i need to run migration all over or do i need to do something else ? I have (migrations folder )?? all help is welcome*
I did export and import data base in MySql

The following will update the database to the latest migration
Update-Database
If that does not work, you can apply the migrations individually
Update-Database -TargetMigration Initial

You can specify which migration to run like this:
update-database -TargetMigration:201405131513421_Inital
And then you can update to latest version like this:
update-database

Related

EF Core ASP.Net Core edit migration

If I need to change the database migration, should I add it to the existing migration in the column table or do I need to change the data in the designer? It is important to save the data and keep the startup migration ready.
In practice i recomment you not touch the migration,instead run the command Remove-Migration, edit the Configuration of the entities and run the command Add-Migration <migration-name>.
Or if the app is in development phase simpply use the following steps:
1-remove the folder migration
2-in the nuggets terminal run Drop-Database command
3-run Add-Migration <migration-name>
4-run Update-Database
Then you will recreate the database with the modifications.

How can I change foreign key option on MySQL EF 6?

I created a table with wrong foreign key option in MySQL EF 6 so I want to change foreign key option of that table to CASCADE DELETE, but how to?
I made this database on code-first. Is there any solution without re-creating database or manually call SQL?
There is a solution. You can customize the migration code using one of this commands
in Visual Studio
Add-Migration Changes
using PowerShell window
dotnet ef migrations add Changes
Then you can update the database again.
Here's more information
Just change to mapping as it is needed to be
then create migration script as you did it earlier.
add-migration AlterMyTableFK
after that Update Database as you did it earlier.
update-database
Just make changes you want, then in the "Package Manager Console", execute this line:
add-migration v1
note that v1 in the above line is for migration versioning and you may name the migration anything you want.
To add a new migration that reflects your changes. then execute this line:
update-database
You Can Make changes You Want, Then Open "Package Manager Console"
PM> Enable-Migrations
then :
PM> Add-Migration v1
then:
PM> Update-Database

EF Migration object already exists error

I am working on an ASP.NET MVC project with Entity Framework with code first from database. I get the models for each table in the database. I made some changes in the models, enabled migrations and when I initial the migration I get an error:
There is already an object named 'TableName' in the database."
I tried with update-database -force but didn't help. The initial migration creates the tables that already exist!
How to make the initial migration apply the changes on the models and not create the tables from beginning?
And what is the best practice to sync changes between database and models in this case?
try to Run the
Add-Migration InitialCreate –IgnoreChanges
command in Package Manager Console. This creates an empty migration with the current model as a snapshot. and then Run the
Update-Database
command in Package Manager Console. This will apply the InitialCreate migration to the database. Since the actual migration doesn’t contain any changes, it will simply add a row to the __MigrationsHistory table indicating that this migration has already been applied.
see this
then change your models and add migration.
another approach is to simply comment all the code on up and down methods
Best and working For me idea is to comment all the code in UP and Down functions of Initial migration file and then fire
dotnet ef database update this should work fine,make sure you update migration before commenting out initial migration
If none of those answers work for you, you may check if in your code you use the method
context.Database.EnsureCreated()
Be aware that that method doesn't apply any migrations
(source) (you can check this making a sql query to the database and like "select * from __EfMigrationHistory" and be sure that there are no migrations on that database.
EF provide a really good method to help this that is
context.Database.Migrate()
that no only it will ensure that you have a database, but also use migrations and migrate yout db to the last version.
Hope it helps
This error appears when you deleted previous migrations and your new migration try to create new table that already exist. Recover previous migration and everything will be ok.
Amr Alaa solution works, but the database didn't migrate to the latest model.
Here's how it worked (using automatic migration) :
Delete Migrations folder
Execute enable-migrations
Set this two properties to true in the newly created Configuration.cs
public Configuration()
{
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
}
Execute Update-Database -Force
Your database will be updated to latest scheme and ready.
hope this helps.

Where is the mysterious EntityFramework database?

In my main project, I have configured a connection string to my database like this:
<add name="DefaultStoreConnection" "provider....">
In my infrastructure project, I have a database context with a default constructor that passes the connection name to the base class:
public DatabaseContext() : base("DefaultStoreConnection") {}
As soon as my application starts, EF generates a 'store.sdf' (SQLCe database) in the application output folder (\bin).
Now, I wanted to reset all migrations and start with a plain database. I deleted the 'store.sdf' in the \bin directory, deleted all migration files and then called in the Package Manager Console:
Enable-Migrations -Force -ProjectName "MyInfrastructureProject" -StartUpProjectName "MyMainProject".
This worked fine, a new migrations configuration class was generated. Then I ran:
Add-Migration Initial -ProjectName....
And then the following line appears:
A previous migration called 'Initial' was already applied to the target database
Where? Where does this migration has been applied to? Where can I reset this 'migration'?
It should be in a folder containing all the migrations, but it's possible that it's deleted after it has been applied to database. You can see list of applied migrations in __MigrationHistory table of your target database. If you wish to rescaffold the database, empty the __MigrationHistory table.
I run to the same problem as you. After a day of google search without a single useful help, i figured it out myself that this is Visual Studio bug where your solution have more than one Mvc projects. The add-migration will check on the older Mvc project to create migration file for your current project (that is why i call it BUG).
To avoid this, you should only use one Mvc project per solution.
Have a look at the app_data folder of your project.

Code first migrations - how to display pending model changes?

I'm using code first migrations. Is there a way to display pending model changes in package manager console before I scaffold a new migration?
The accepted answer tells how to get the SQL for a already scaffolded model change before applying to the database.
The original question regarded model changes pre-scaffolding (i.e. changes in the model since the last "add-migration" before running the next "add-migration" ...)
To that answer i will just say: scaffold anyway, that gives you your preview. By that i mean, run "add-migration preview" or something similar, it will create the scaffolded migration with the model changes that you are interested in. Then just delete if from your solution ...
The point here is that there is no need to "preview" when actually "doing" can be quickly undone. Some might think deleting a scaffolded migration version from the migrations section of the solution would break something, but no it is very well supported.
You can even test scaffold, then create the sql script as Colin suggest in his answer, to get the full SQL. Still nothing has been done at this point, so delete the migration version if you'd like.
There is no way that I know of to view pending changes in the model before scaffolding the migration, but I can't think of a reason not to scaffold the migration using Add-Migration so that the pending changes can be viewed in the migration file. There is no need to apply those changes to the database and the scaffolded migration can always be deleted.
Once the migration is scaffolded, if you use Update-Database -Script entity framework generates a SQL script rather than executing the changes directly.
You can get help on the EntityFramework in the package manager using get-help EntityFramework
And you can get help on the Update-Database command using the following:
get-help Update-Database
get-help Update-Database -detailed
get-help Update-Database -full

Categories

Resources