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.
Related
I am trying to set up integration testing between the repository and database for a .NET Framework app, which has already been built. I have been trying to find a way to setup and seed a test database or in memory database that I could use to run some tests against, but don't see much in terms of .NET Framework apps.
I have seen that there is a Microsoft.EntityFrameworkCore.InMemory library that accomplishes what I need, but it is only available for .NET Core projects running version 6.0. I have also found little to no similar mention of this being possible on a .NET Framework Project, but imagine that there has to be a way.
My work on this legacy app is my first foray into the .NET world, so I may be misunderstanding things from my research on this topic. Is it possible to set up a test/in memory database for integration testing a .NET Framework app? If so, what are the best practices for doing so?
If you really want to test a pure SQL database, my advice would be to setup a local MSSQL Server for testing your application and its integration with the database. I don't know what kind of project you have, but assuming it is a ASP.NET project (Web API p.e) you should already have well defined Models which work like the building blocks of the tables you will use on your database. EF Core essentially maps those models as tables in the database.
Define a new connection to a local MSSQL Server
1. Define the service on Startup.cs
using Microsoft.EntityFrameworkCore;
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<DatabaseContext>(opt => opt
.UseSqlServer(_config.GetConnectionString("Name"), builder =>
{
builder.EnableRetryOnFailure(5, TimeSpan.FromSeconds(10), null);
}));
}
The EnableRetryOnFailure will make sure your application retries a connection to the database server in case of a connection error.
2. Define a ConnectionString
Go to appsettings.json and add
"ConnectionStrings": {
"Name": "Server=(localdb)\\MSSQLLocalDB;Database=myDb;Trusted_Connection=true"
}
This example uses Windows authentication, but you can use SQL Server Authentication
3. Install MSSQL Server Management Studio
It helps a lot for visualizing your database tables
Entitiy Framework in-memory is available for .NET 5.0. My advice to use a local database instance with MSSQL is Microsoft's own recommendation, as mentioned in their website
The EF in-memory database often behaves differently than relational databases. Only use the EF in-memory database after fully understanding the issues and trade-offs involved, as discussed in Testing code that uses EF Core.
I am looking for an option to have a free database on Azure.
From the article I see that there is an option to have free DB on Azure. But it works for standard ASP.NET MVC. How can I achieve this in ASP.NET Core ?
Or, maybe Azure provides some free/very cheap DB?
there used to be a free SKU for SQL server, but's no longer available. the only free db option you have atm is ClearDB's hosted MySQL (Mercury tier). if you have the correct DB drivers or framework then you can do this easily : https://docs.asp.net/en/latest/data/index.html
Hope this helps
Once you have created the free database you can use it for any purpose you desire.
You can get the connection string and add allowed IP addresses through the Azure portal.
During development I am using SQLLite, which is free and works like a charm. It is perfect for small applications. Using it with Entity Framework however might cause some issues when database changes are introduced later. But once you switch to production you can just use a different database provider.
I've got a multi tenant asp.net mvc app hosted on iis 8. Each tenant has it's own database, an there is one database with tenants metadata like name, database-name...
As ORM i use Entity Framework with Code-First Migrations.
Now I've got a problem when update the application. because when i start the migration, the App is offline for all tenant because, entity framework can't connect to database with different migration version.
When all Databases are migrated i can start the IIS-Website and all Tenants come online again!
Is there a way to achieve such database migration? OR Am I able to host multiple versions of an MVC App on same Domain (perhaps routing per e.g. http-Header or meta Database)?
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
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.