EF Core Migration - multiple databases - multi-tenant

Is there a way to run EF Core migrations on multiple databases having the same set of tables. This is for multi-tenancy architecture where there's a master database (has metadata of all tenant databases including the tenant database connection string) and one database per tenant having the same set of database objects. We need to be able to run these migrations when a new tenant database is created automatically in SaaS model and also run these migrations whenever there are changes to the database structure (new columns, data type changes, new indexes etc.)

I've posted this exact same question on EF Core's GitHub.
The answer is, it can't be done at design time. You basically need to run your migration scripts manually on each tenant's database.
Executing migrations at runtime, however, is easy. You can instantiate a dbContext for each of your connection strings when your app launches (before WebHost.Run() if it's a web app) and execute your migrations like this: dbContext.Database.Migrate();
This is not ideal, of course, because it makes it harder for you to rollback your migrations to a certain point using Visual Studio Package Manager Console or CLI using dotnet ef commands.

The CLI command can be provided a connection string. So you could run it once per db, providing the connection string for each.
The command would look like this:
dotnet ef database update --connection "Server=client1.db;Database=client1"

Our team has about 10 developers, our application is one front-end connect to 20 databases(same scheme), and new database will be add when there is new client. Time to time someone will need update DB scheme, we end up doing this.
if you need scheme change, create SQL script and create the change request by email
only one person in the team run those script, and update database access layer
git push
tell the team dinner is ready
The person doing this created a EXE project for DB migration, he keep adding script to a folder, so the folder will contain all the script
0001.InitTables.sql
0002.MoreTabels.sql
0003.UpdateDropdowns.sql
.
.
.
then he use a library like DbUp (https://dbup.readthedocs.io/en/latest/) help him track those scripts and run on DB server.
He will run for DEV server first, on the release date, he will run this for production.
List<string> connectionStrings=new List<string>{
"ConStr1","ConStr2", "ConStr3"
};
foreach(var conStr in connectionStrings){
var upgrader = DeployChanges.To
.SqlDatabase(conStr)
.WithScriptsEmbeddedInAssembly(
Assembly.GetExecutingAssembly()
)
.LogToConsole()
.Build();
var result = upgrader.PerformUpgrade();
}

Related

How to stop Auto Migration of Database table in Go

I am trying to build an api in beego. I have to make a controller and a model for this project. Model has a database table but the table is not in my database. Beego automatically creates the table in the database. How can I stop it.
I imagine you are using this command err := orm.RunSyncdb(name, force, verbose). This command looks for changes in your models and if there are any drops the database and creates a new one. If you want to stop it simply don't use this command and write database migrations for your app with bee generate migration migration_name. In the file you have to run the required sql to create the tables. After writing your migrations run bee migrate -conn="{your_database_connection_string}".

Production Database Creation / Migration dilemma for ASP.NET Core MVC

I have been building my ASP.NET Core MVC web application since last year and there are currently 100 migration files in my project, as the database has evolved along with features and capabilities. My development / test database is obviously in sync with this migration.
Now, the time has come to create a Production environment, and I want to have an empty database with only the table schema. So, I just can't copy my test database to create a production database.
So, the question / dilemma I have regarding creating this Production database is as follows;
1. I can either create my Production database by running Add-Migration command (for which I need to delete existing migration files from the project), or
2. Create table schema in SQL Management Server, and keeping the __EFMigrationsHistory from test database
With [1], I am not sure how I will be able to manage my test database with this same project, going forward.
With [2], I am not sure if there is any drawback.
So, what is the standard or best practice for Production deployment?
You can certainly use Visual Studio to create or update database tables, but this would normally just be in your development environment.
If you are worried about the number of migrations you have, you still have the option of starting over by deleting them, and deleting your migration history (and manually deleting your tables and data) and creating a fresh 'Initial create' migration. If you do this you will probably want to export any test or config data first, or make sure you have a way to recreate it.
For the staging and production deployments, best to use SQL Server Management Studio to generate a script to build your tables. The database administrator - or you if it's just you - can create the database and run the script to generate the tables.
This is because staging and production environments tend to be more tightly controlled than development, so best to understand the processes that are most appropriate for the environment.
You need to check that your dev and staging/production SQL databases are set at a matching 'compatibility level', and also need to decide whether you need to add any seed or configuration data on create.
In SQL Server Management Studio, select the appropriate option to generate a script for the required tables.
https://learn.microsoft.com/en-us/sql/ssms/scripting/generate-scripts-sql-server-management-studio?view=sql-server-ver15
You can also use this process to export, and import data - this is ideal for config or test data.
https://dzone.com/articles/generate-database-scripts-with-data-in-sql-server

Save data from database

I have created an application with internal database LightSwitch..
Now I want to publish my application and I want to publish also data of my internal database..How can I do?
for example : I have an application Fantacalcio and I created some players in my internal database of lightswitch..now when I publish my application and I install it in my pc there are no data in my application.. I want that when I install my application there must be players that I have created before..
You can do it programmatically in something like Application_Initialize, or in a SQL script.
LS has no "built-in" way to pre-populate data, so it's a matter of choosing a workaround.
One possible way is to do the following:
Attach the lightswitch internal database to SQL server
Export all the data into a SQL script, here are the instructions
After you have the sql script (mostly INSERT statements), then run
the script on your designated database.
The exact same data should now be populated there.

Deploy Entity Framework Code First Database

I have an ASP.NET MVC 3 application using an Entity Framework (4.3.1) Code First database. Now I would like to create a comprehensive zip file containing the database, the application package generated by Visual Studio 2010 and a script to deploy everything to a Windows 2008 server with IIS7 and SQL Server 2008 with a prepared (but empty) database.
I don't foresee any problems with the deployment of the application package, but I'm unsure of what approach to use in deploying the database. The target environment already has an empty database that's been assigned to me, but I've been told that dropping and creating the database is fine.
From what I've read, I can do a straightforward copy of the .mdf and .ldf files to the server and then setup my connection string to point to that specific file but this approach sort of ignores the database that has already been created (or at least named) for me. The other approach would be to use the the existing .mdf to create the database on the server with a script. My only issue here is that I would like to keep the database name assigned to me.
I usually connect to my development database locally using SQL Management Studio and right-click the database, choose Tasks -> Generate Scripts. Then I select the entire database or just the tables I'd like to keep, click next, then click the Advanced button and make sure that I am scripting out "Schema and Data", and then generate a sql script that I can run on the production database, therefore keeping the table structure and the data that was in the dev database. Obviously, if you don't want to keep the data then just script out the Schema only. Then, point your application's connection string to the new production environment database and you're good to go.

Multiple Sql Server 2008 Database Projects, do they go in a Sql Server 2008 Server Project?

I'm a bit new to database projects, but I find that with a bigger team it is a good way to synchronize development.
I had a single Database Project that contained all the tables used by a web app.
The powers that be want to display data from another database in the web app.
I create another Database Project that represents the tables I need in the other database.
I create views in the original database that represent tables in the second database (for linq to sql joins mainly)
Questions
Do I create a Sql Server 2008 Server Project and merge both of these databases into it? From what I can tell, a Server Project still only represents a single database.
If a Server Project isn't meant to represent multiple databases, what is it for?
I have added steps in our TFS Workflow to deploy changes from the first Database Project into our Dev environment on check-in (CI). Assuming a Server Project is meant to contain multiple database projects, can I deploy it and have it deploy changes in both databases?
Thanks!
A server project is for server scope objects needed by your solution: endpoints, server principals (logins), server level permissions, linked servers and other similar constructs that are not scoped to any particular database.
Seems to me that what you need is a solution with two projects:
your original database project
you reference database project
You need a third project in this solution, a server project, only if you must add server scoped objects.
For more details, see Using References in Database Projects.

Resources