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

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

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 to generate Entity Framework Objects from script

I am working on a entity framework project generating the Entity classes using the New-> ADO.NET Entity Data Model -> Code First From Database. I then select nearly 100 tables to generate (not all tables are suitable to go in the model).
Problem is I am regularly updating the schema, and the only way to refresh the model seems to be to delete and start again, which is fine except I have to re-select the nearly 100 tables again.
Is there any easy way of scripting this generation process?
You should look into using Entity Framework Migrations and start doing your schema changes from the code itself
Set the CompanyName.ProjectName.Infrastructure.EfRepository (the project which has your DbContext) as start up project of the solution
Open the Package manager console
Choose CompanyName.ProjectName.Infrastructure.EfRepository as default project
Run the following commands:
Enable-Migrations -ConnectionStringName "EfDataRepository"
Add-Migration Initial -ConnectionStringName "EfDataRepository"
Update-Database -ConnectionStringName "EfDataRepository" -Script -SourceMigration:0
Then delete the auto-generated Migrations folder within the EF project!
Where EfDataRepository is the connection string name.

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.

How to tell NuGet for past migration

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

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