Changing Production database using Migration Script in MVC Codefirst - c#

I Have Developed a Web project using MVC 3 CodeFirst Approach and VS2010.After several Months I need to Change DataBase Schema(of course without loosing Data)After searching the web I found migrations which Enable Me to generate change script which seems to work well on local machine.But My Question is How can i use this script on production server?
Or Any Other Solution for Modifying MSSQL database on production server.

As Mark Mentioned , using MigrateDatabaseToLatestVersion Initializer we can Update Our Database . and it worked for me.

Related

Best practices for Database Migrations in Production in Azure

I am developping an ASP.Net Core application, backed with a PostgreSQL database.
So far, my application is hosted on my personal VPS, but I would like to migrate on Azure, and I don't know how to handle the DB migrations over there.
My database scheme is handled by Entity Framework Core and mapped with POCOs.
As explained in the docs, when I want to change my SQL scheme, I update the POCOs, create a migration C# file through the dotnet ef migration create command, and apply them through the Database.Migrate() command at runtime.
This is defined by Applying migrations at runtime, however as stated:
While productive for local development and testing of migrations, this approach is inappropriate for managing production databases.
There is a paragraph about using SQL Scripts, but nothing really about "How to apply migrations scripts in CI/CD pipeline".
Of course, I had a look to the official tutorial on Microsoft docs about how to deploy and ASP.Net Core app + SQL DB on Azure, but there is nothing about DB migrations (the migrations of the production DB are applied through CLI from local computer ...).
Isn't there a specific service on Azure allowing us to apply the DB migrations?
I know this question is specific neither to Azure, nor to .NET apps, but I was afraid my question would be too "broad".
Also, when looking for "DB migrations" on Google, most results are concerning the migration from a given DB to another (or from a vendor to another), which is not the same.
Many thanks
There isn't a specific service in Azure that manages your DB migrations.
From experience, it's usually managed by generating the migration SQL (dotnet ef migrations script), reviewing it and then running it manually or saving to a file that is then executed as a part of the CI/CD pipeline.
I guess you are looking for Azure Database Migration Service. It has two main mode, online and offline mode depending on your migration strategy.
It is compatible with PostgresSQL et should allow migration to managed Postgres (Single or Flexible Server).
https://azure.microsoft.com/en-us/services/database-migration/#overview
Once your migration is done, you can then point your connection string to your newly migrated DB.

How add new column to deployed table in Azure without losing data?

I have already deployed ASP.NET MVC web application with DB in Azure. I use EF Code First.
Now I need to add column to table Users without losing any existing data. Should I use migrations or SQL Server Management Studio or something else? And how should I use it?
I had to do the following:
enable-migrations
add property into the entity I wanted to change
add-migration
deploy

ASP.NET MVC deployment with multiple migrations

I've been practicing on MVC programming and now I've reached a place to deploy my web application.
I've tried deploying on local IIS and the only error I found was because of my localDB (probably connection string, that I don't really know what to do about it)
I'd like to know how to edit the connection string and my database for deployment,
and by the way, to know if it's possible to deploy web application with more than one DB migration configs or not? if yes, how? (what should be considered while deploying?
Here I found the best solution for my problem.
by
Anthony Chu
Multiple DB Contexts in the Same DB and Application in EF 6 and Code First Migrations

Table already exists in database (only when published on Azure)

Everything was working fine in my ASP.Net MVC5 EF6 project until I had database issues. The only way I found to resolve them was by deleting my database and migrations folder. I recreated everything from scratch and it fixed every issues I had locally (localhost).
Then I tried to publish my web app to Azure and now I get this error every time I try to Login in my App (first time my context is used):
There is already an object named 'Companies' in the database.
I searched for hours on the internet about this and found nothing about this bug when it only happens when the database and web app is published (because it working perfectly fine on Debug locally).
I have read plenty of theories about migrations being messed up so I deleted my sql databse AND my Web App on my Azure portal and created new ones. Even after this, I am still getting the error.
So it seems I have an issue in my code and I know EF6 is trying to create this database table a second time but I found no places in the code where it could do that (even less where it only happens when published). I must also tell you that nothing changed in the WebConfig and in my DbContext. Only my models changed and my Migrations folder.
I am definetly missing something here and I would really like some help to understand EF6 better. I am new to this technology and I find it very useful when it's working but when you have bug, it becomes very very painful though.

C# EF Code First Deploy to Godaddy

I am having issues deploying my app to godaddy hosting using EF code first. I dont want my app to create the database since the database already exists.
So I read some posts on here and placed this in the constructure:
Database.SetInitializer<myContext>(null);
Now I get a permission error writing to the temp file.
Anyone had a successful experience deploying this to godaddy shared hosting?
When the project used Model first and edmx files it was deployed fine.
I was able to get this to work right after posting this..
All I had to do was move:
Database.SetInitializer<myContext>(null);
to the global.asax Application_Start
Can you post what's the server error you are receiving.
setting db initializer to null should work fine. I have tried that. If you post the exception, we can probably find out why exactly that happening
You should use a generated database from the development and remove the database initializer from your code. From my experience, I always got a problem if I didn't remove the database initializer.
I've faced kind of problem before. I am using a different hosting site though. I deployed my MVC 3 Code First app back then. When I deployed the app, I didn't remove the database initializer, so the app drop the database and try to create it again. But when it create the database, it got a permission error. The problem is, the database is actually dropped in the server but in the hosting site cpanel, it is still exist. I send a ticket to the support, and problem solved.

Categories

Resources