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.
Related
Just downloaded the vs community edition 2019 version 16.7.7 and it flag off an error saying couldn't download microsoft.net.4.7.targetingpack/resources however I launched the vs community 2019, started a asp.netcore web application and wrote a single class and scallfolded a controller with a CRUD view but my problem is I can not connect to the database.
My connection string is fine at the appjson setting also my startup.cs because I have checked all the suggestions offered here, and I implemented them! Please help! Is my vs-2019 corrupted.
I want to say thank you everyone for listening to me, however I figured out that I didn't generate a database creation code by enabling migration and adding a migration which will do that automatically and save a snapshot schema that will be used to create the actual database in the database management environment. Please that was my mistake. Add migration and update database and your good to code some more
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
I have a visual studio asp.net web application that uses entity framework to connect to the database. I follow this tutorial http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/migrations-and-deployment-with-the-entity-framework-in-an-asp-net-mvc-application, and enable code first migrations. It connects to local sql server fine and the development is ok.
When I publish this application, I have to use a mysql database. I can't run code first migrations on this database.
I have two problems:
I want to use mssql database in development, and mysql database for production, how do I specify two different configs for that?
If I follow this tutorial http://www.codeproject.com/Tips/788357/How-to-set-up-application-using-ASP-NET-Identity-w, to setup mysql, how do I switch back to mssql for my development?
You need to specify two different web.configs. One for dev and one for prod. See this link on how.
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"
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