I'm trying to enable migrations on a Firebird 2.5 database.
I'm using VS2015 and the ADO Driver and Entity Provider are installed and working correctly.
I reverse engineered the database, made the necessary changes to make it work.
I can do the enable-migrations on the Package Manager Console, and add a migration.
When I do update-database this happens:
PM> update-database
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Applying explicit migrations: [201705301505265_Initial].
Applying explicit migration: 201705301505265_Initial.
ScriptHalted
If I do a update-database -Verbose I get this:
PM> update-database -Verbose
Using StartUp project 'Visao.Web'.
Using NuGet project 'Visao.Data'.
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Target database is: 'D:\Upwork\Fernando\SIGECOM.FDB' (DataSource: localhost, Provider: FirebirdSql.Data.FirebirdClient, Origin: Configuration).
Applying explicit migrations: [201705301505265_Initial].
Applying explicit migration: 201705301505265_Initial.
INSERT INTO "__MigrationHistory"("MigrationId", "ContextKey", "Model", "ProductVersion")
VALUES (CAST(_UTF8'201705301505265_Initial' AS VARCHAR(150)), CAST(_UTF8'Visao.Data.Migrations.Configuration' AS VARCHAR(300)), x'1F8B0800000000000400ECBDDD92DC3
and a lot of character because its trying to add a blob to the database.
The __MigrationHistory database was added to the FDB file:
So I guess it's kind of working.
I have no idea what to do next, anyone?
EDIT
Since everything seems to be created, I think the error might be from the Insert on the Migration Table.
Maybe a bug?
EDIT 2
I created an SQL script with Update-Database -Script and tried to run on IBExplorer and get the following error:
can't format message 13:896 -- message file C:\WINDOWS\firebird.msg not found.
Dynamic SQL Error.
SQL error code = -104.
Unexpected end of command - line 1, column 226.
Even running just the Insert statement is not working.
EDIT 3
Create an issue in their tracker to see if someone else knows
EDIT 4
It seems that the guys responsible for the Bug Tracker page keeps removing the bug.
Anyway, if I change providers, either MSSQL or MySQL, the migrations works.
EDIT 5
This is the first migration created. It is empty because I'm trying to use an existing database, already populated, and now I want to use Code First.
public partial class Initial : DbMigration
{
public override void Up()
{
}
public override void Down()
{
}
}
As Mark Rotteveel said (and he should get points for this answer). You're hitting the limit for a statement size in Firebird. There's not much I can do with it because this needs to be script (even if it's executed directly), so parameters won't work.
Possible workaround would be to create multiple smaller models and use these for migrations. Or always generate the script into file and manually use parameters.
Related
In my ASP.NET Entity Framework 6 project I added a new Model and Controller.
I have automatic migration enabled but allowed data loss disabled.
When I ran the application after these changes the new table was actually created into the db, but the application told me:
Automatic migration was not applied because it would result in data loss
Without any other detail. As read in other questions I tried to issue:
Database-Update -Script
to get the "differences" but nothing happened, just the same error message.
I really don't want to force the update unless I'm sure what it's going to do.
What should I do in order to retrieve the content of the current migration?
I tried to look into the dbo.__MigrationHistory table but the Model field contains binary data.
UPDATE
I was able to decode the Model field using this method, but the edmx file contains just the whole diagram of the db, not the actual commands the the migration would issue.
Furthermore, even if add the -Verbose flag as suggested by the tool itself:
PM> Update-Database -Verbose
Using StartUp project 'MyProject'.
Using NuGet project 'MyProject'.
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
It does not show the SQL statements...
The message is displayed because you probably made some change to your model that results in a DROP command (e.g. removing property).
To see the script for that specific migration, i.e. the update between the latest migration and the one before it, you can use the following command:
Update-Database -Script -SourceMigration: <migrationBeforeTheLastOne> -TargetMigration: <lastMigration>
as stated in ChW's answer.
I have two solutions, the base solution with no migrations(which is in production, so I can not wipe the data) and now I have branched of this to set up migrations and make some model changes.
First I need to set up migrations on the branched solution (second solution) so that I can apply the model changes so I:
Turn on migrations which auto creates an InitialMigration
Enable-Migrations -ContextTypeName Context
Make a blank migration based on the suggestion from https://www.apress.com/gp/blog/all-blog-posts/secular-trends-for-the-cloud/12097630
Add-Migration InitialBlank -IgnoreChanges
Update Database * update-database*
Make my model changes
Add a migration containing my model changes * Add-Migration add_entity*
Run Update-Database
So I delete the database created as I need to run the first solution to create the initial db setup (to mimic live).
When I run the first solution it creates an entry in MigrationsHistory table named InitialCreate (201807061432030_InitialCreate), which has been auto created. I then run update-database on the second solution which applies my model changes fine although their are discrepancies in the InitialCreate MigrationId.
Migration entries in my second solution (in order they were created and the order they are in the solution):
- 201807061257015_InitialCreate
- 201807061315294_InitialBlank
- 201807061323086_add_entity
Migration entries in the migration history table after running the first then second solution:
1 | 201807061315294_InitialBlank
2 | 201807061323086_add_entity
3 | 201807061432030_InitialCreate
The second solution runs fine but when I try to add any data I get System.InvalidOperationException: 'The model backing the 'Context' context has changed since the database was created. Consider using Code First Migrations to update the database.
I have tried to create another migration on the second project to make sure their are no model changes that have not been migrated (their shouldn't be) but I get an error.
Unable to generate an explicit migration because the following explicit migrations are pending: [201807061257015_InitialCreate]. Apply the pending explicit migrations before attempting to generate a new explicit migration.
My questions:
How do I resolve the MigrationId mismatch? As the first solution did not have migrations turned on and when I do turn them on (in the second solution) it creates a new id.
Could the exception System.InvalidOperationException: 'The model backing the 'Context' be thrown by the MigrationId mismatch or could anyone point me at why this could be happening? I have had a look into this error but the solution's I found don't seem to work:
Deleting the migrations history table is no good as I can not do this in production because of customer data.
The other solution I found of adding Database.SetInitializer(null); to global.asax seems to make no difference.
Why does the order of my migrations look different once they have been applied in the migration history table?
Thanks in advance!
I am not sure if this will work but I think I may need to manually delete the first InitialCreate and edit the db entries to reflect the correct InitialCreate. Based of this SO question I just found https://stackoverflow.com/a/13108243/3172635.
I will have to try this Monday though.
Edit: I first tried deleting the InitialMigration entry in the _MigrationHistory table but this does not work as it tries to apply the InitialMigrations again but cant as the tables are already created. So what I did was update the MigrationId for the InitialMigration entry to reflect what it was in the new project.
The sql I used:
SET MigrationId='201807061257015_InitialCreate' WHERE MigrationId = '201807061432030_InitialCreate'
I am trying to follow the tutorial from the link below to create an app that shows a list of movies from a database with crud functionality, but I am trying to add a user login.
https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/
I have been trying to figure out how to add and update the migrations for the Movie database. I am able to do it when I follow the tutorial without the user login, but once I add the user login, there seems to be an issue that I am not catching.
After I create the model class, I am trying to apply the following steps for the migration
Goto the command line and access the folder with the project in
it
apply the following commands
dotnet restore
dotnet ef migrations add --context MvcMovie2Context (When I run this command, I get (Missing required argument ''.) error)
dotnet ef database update --context MvcMovie2Context
(This command seems to be working for some reason)
When I run the app and click on the movie link, I get the following error
SqlException: Invalid object name 'Movie'.
System.Data.SqlClient.SqlCommand+<>c.<ExecuteDbDataReaderAsync>b__107_0(Task<SqlDataReader> result)
I have looked through my Model, View and Controller classes to check if I made a typo or a syntax error that would cause the object name ‘Movie’ to be invalid, so I think it means that my Movie database was not created.
I have restarted the project multiple times following the tutorial
I have attached screenshots of the command line errors and browser error. I would greatly appreciate if anyone has any hints or suggestions that they could provide me. Thanks!
I had a similar error even though all the tables were properly created. It turned out that my connection string was wrong.
Since the error is SqlException: Invalid object name 'Movie', that means the table named 'Movie' has not created or the Database you are referring has not created.
To see if the Database or table 'Movie' has created, open SQL Server Object Explorer and check the Database name is the same as in appsettings.json. If the Database or table has not created, you may need a migration.
To migrate, open Tools-> NuGet Package Manager-> Package Manager Console and hit the following commands:
Add-Migration MigrationName
Update-Database
Then check SQL Server Object Explorer again or build and run your project.
Do you have a Movies or Movie table in your database?
Since you say that dotnet ef migrations add --context MvcMovie2Context fails, leads me to believe that your model has not been reflected in the database schema. Also, as far as I know, you need to provide a migration name, which is probably why you are receiving the error.
Please try running the following commands.
1.dotnet ef migrations add "AddedMovieEntity" --context MvcMovie2Context
2.dotnet ef database update --context MvcMovie2Context
Hopefully this time step 1 will run successfully, from there you should see a migration file that includes the schema for the Movie entities. After step 2, you should see the respective table in your database.
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.
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