I want to get the sql of some migrations that were already executed on my developer machine, so I can execute them on some other enviroment(qa, prod, ...).
I found this page http://msdn.microsoft.com/en-us/data/jj591621.aspx (section Getting a SQL Script), but this works only if you have pending migrations. I tried:
Update-Database -Script -SourceMigration: $InitialDatabase -TargetMigration: SomeMigration
but got Target database is already at version SomeMigration.
Can I get the sql of already executed migration?
EF6, MVC5, VisualStudio 2013
Well, it does exactly what it should since you invoke Update-Database on your local environment.
If you need to generate update script for another environment I see the following options:
You can target your local application to the database from remote environment. In this case it is enough to change connection string in your web.config manually.
You can revert your local database to the state of the database that your remote environment is targeting.
Update-Database –TargetMigration: "last migration on remote environment"
Related
So I have an ASP.NET MVC application that I deploy on the IIS using the Publish feature inside of Visual Studio. I have performed some changes to the existing version of the database and the application - this is what I did: I made very little changes to the database on the production server (just added a single column to two tables and made them FK to another table). Then I took the generate script from this production DB, went to the local (development) SQL server and ran the generate script to create the database locally. After this, scaffolded this local DB instance into VS (to get the modified model classes locally). Then, after some smaller modifications to the app (I was basically just adding some document upload functionality), I did the Publish and deployed that on the production server.
However, I ran into the following error when I tried to log in the website: error screenshot
Why does this happen? How can applying the migrations work through Visual Studio package manager command if I run the suggested Update-Database command locally (and from there I cannot reach the target database)?
Thank you very much for your help.
You need to add migration for your changes, as below :
Add-Migration Version2
it generates migration files, then you can run
Update-Database
to apply changes in the database.
You can not manually migrate the database changes. It should be under Add-Migration command to create a new migration version.
I have the PostgreSQL database on remote server. And when i'm trying to update entity model in my web-project from database in visual studio i get such error:
Unable to convert connection string for execution time to its
equivalent for development time.
There is my connectionstring:
metadata=res://*/Model.MerModel.csdl|res://*/Model.MerModel.ssdl|res://*/Model.MerModel.msl;provider=Npgsql;provider connection string="PORT=5432;TIMEOUT=15;POOLING=True;MINPOOLSIZE=1;MAXPOOLSIZE=20;COMMANDTIMEOUT=20;COMPATIBLE=2.2.5.0;DATABASE=****;HOST=***.***.*.**;PASSWORD=********;USER ID=postgres""
Connection parameters are correct. Using them I can connect to a database in PostgreSQL Client. Help me, please. What is the problem?
UPD: Finally i found solution for my problem. I deleted Npgsql and Npgsql.EntityFramework packages from the project (to make clean installation of new versions). Then i installed Npgsql v.3.2.7 and EntityFramework6.Npgsql v.3.1.1. After this i deleted "COMPATIBLE=2.2.5.0" from my connectionstring. Then it was necessary to rebuild all the project. Note that Npgsql PostgreSQL Integration VSIX must be installed in your Visual Studio. Finally i can update my EF model from the database and check if it is correct after this manipulations.
Is it possible to generate migration script using EF code-first add-migration (on dev machine) and use it on a prod SQL Server which is accessible only by VPN (client internal network)? We cannot connect from dev machine to prod DB so we aren't able to invoke the update-database command.
If not do you know how to generate insert/update queries which can be used on production SQL Server Management Studio?
Thanks,
Piotr
Briefly
You could run the following command in the PM Console if you'd like to have all latest changes represented as SQL script.
Update-Database -script -SourceMigration
NameOfTheLatestMigrationOnTheServer
In detail
You can generate SQL script manually, just run the following commands:
Update-Database -Script
Code First Migrations will run the migration pipeline but instead of actually applying the changes it will write them out to a .sql file for you. Once the script is generated, it is opened for you in Visual Studio, ready for you to view or save.
You can specify a source and target migration to generate the script for.
Update-Database -Script -SourceMigration: "LastMigration" -TargetMigration: "MigrationTo"
If you don’t specify a target migration, Migrations will use the
latest migration as the target.
If you don't specify a source
migrations, Migrations will use the current state of the database.
Investigate Getting a SQL Script section for more details.
If I understood you correctly , you want SQL script from EF and you want it to execute at Production.
If yes , then go for running the following in the Package Manager Console
Update-Database -Script
-Script : Generate a SQL script rather than executing the pending changes directly.
Once the script is generated, it is opened for you in Visual Studio, ready for you to view or save
Apply code first migrations is easy working locally. Just type "update-database" into Package Manager Console to update the local database.
However, what about to update the remote database? How can I apply migrations to it?
You can change the connection string in your web.config file to point to the remote sql server, and then run the same update-database command.
Alternatively, you can generate the sql script with the -Script flag when running the Update-Database command. (Update-Database -ProjectName NameOfProject -Script) This will generate the script needed to run on the remote database
While developing an application I used EF automatic migrations. So now when I have deployed my app on VPS, I don't know how to add new tables and fields to my database.
Can I connect to the remote database directly from my project in VS2012, updating a connection string, and update the database using "update-database" in package manager console? Or do I need to install VS on my VPS and update the database from VPS?
My database is already filled with data, so I can't delete it and create again.
Yes you can use Visual Studio, follow this tutorial - it should work for VS 2012 too.
You can also use Code first Migration to update your model by using this command in package manager console:
Update-Database
and you can specify your connection string name:
Update-Database -ConnectionStringName "MyConnectionString"
This is best explained in the link below. Read the "Getting a SQL script" section. That will explain how to generate a script which you can then run on your target database.
This will be necessary if your database access is IP protected for example.
https://msdn.microsoft.com/en-us/data/jj591621.aspx