All,
I am trying to solve a seemingly very common problem of updating our development database and our production database simultaneously, whenever a change is made to our development database.
Something like this:
PM > Add-Migration AddMyNewColumnColumnToMyTable
PM > update-database - dev
PM > update-database - prod
I've seen solutions while researching this but nothing yet that is as simple and straight forward as running SQLCompare on the dev and production databases and then exporting and running a SQL script on the production database.
How are you all doing this?
Thanks
Well as long as both of your databases are following the same migration stream meaning both are created from same migration and updated in same chronological order, you should not have any issues with using same migrations.
What you can do is create two connection strings in your web.config (or app.config) and when you are updating database use the following syntax:
update-database -connectionStringName YourProdDbContextConnectionStringName
So assume you have 5 migrations total: Migration1, Migration2, Migration3, Migration4 and Migration 5.
If your production is updated up to Migration3, and you've made Migration4 and Migration5 on your dev database, issuing update-command with different connection string to your production will apply Migration4 and Migration5 at the same time, without any problems.
Related
We use MSSQL for our C# .NET Framework 4.8 Application using Entity Framework for database related activities.
But on our production environment the SQL server has the Securable: View any database on Deny.
The database for the application exists but Entity Framework cannot see the database and tries to create it, this results in the CREATE DATABASE permission denied in database 'master' error.
I am using CreateDatabaseIfNotExists and MigrateDatabaseToLatestVersion in my Application_Start().
Now the issue (I think) lies with CreateDatabaseIfNotExists.
For the first run we give the db user enough rights to create and fill the database, it does this without problem.
But after the initial setup we remove those rights and the issue starts.
It tries to create the database, But it already exists.
And I am hoping there is a way to have both Automatic database creation/migration, and the View any database on deny securable.
Does anyone have a idea on how to solve this issue?
Is there some sort of option I could enable to stop this behaviour?
You should "wire in" IHostingEnvironment and make sure you run
CreateDatabaseIfNotExists and MigrateDatabaseToLatestVersion
only in certain environments.
===========
For DotNet-Core (NON asp.net-core) apps.
https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.hosting.internal.hostingenvironment?view=dotnet-plat-ext-7.0
for asp.net-core.
https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.hosting.iwebhostenvironment?view=aspnetcore-6.0
....
Then you will use (probably an existing)
"Is" method:
https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.hosting.hostingenvironmentextensions.isdevelopment?view=aspnetcore-7.0
IsDevelopment
IsProduction
IsStaging
or you have the ability to "add your own environment".. with
IsEnvironment(string)
I would NEVER leave to "auto-voodoo" what might happen to the production database.
You can see this approach:
https://stackoverflow.com/a/60399887/214977
We are currently using Azure pipelines to run automated UI and API tests on different test environments. Our tests are written in C# using visual studio, specflow and MStest
It's working well, but now i am looking to improve the test data set up before our tests are ran.
We use SQL server databases. Currently we use a 'Execute single or multiple SQL scripts' Azure Pipeline task to delete data in tables so we can reuse test data in our scripts. We currently only use DELETE FROM SQL commands. For example:
DELETE FROM [dbo].[table1]
DELETE FROM [dbo].[table2]
etc.
Obviously this is not a good solution as it requires maintenance each time there are db changes or new tables and columns are added. We run into foreign key constraints etc.
The application we are running tests on consists or 3 databases:
DB 1 = client db
DB 2 = Broker db
DB 3 = internal db
I was thinking a back up and restore of the test database before we run tests would be a better solution. However, I am unsure what the recommended best practice solution is?
I see Azure Pipelines has these tasks, but i was think maybe a sql/stored procedure script would be better?
Also, can anyone point me in the direction in what a SQL script would look like to do this?
I was thinking a back up and restore of the test database before we
run tests would be a better solution
You can follow below script to backup Azure SQL database in powershell task .
$exportRequest = New-AzureRmSqlDatabaseExport -ResourceGroupName $ResourceGroupName -ServerName $ServerName `
-DatabaseName $DatabaseName -StorageKeytype $StorageKeytype -StorageKey $StorageKey -StorageUri $BacpacUri `
-AdministratorLogin $creds.UserName -AdministratorLoginPassword $creds.Password
$importStatus = Get-AzureRmSqlDatabaseImportExportStatus -OperationStatusLink $importRequest.OperationStatusLink
[Console]::Write("Exporting")
while ($importStatus.Status -eq "InProgress")
{
$importStatus = Get-AzureRmSqlDatabaseImportExportStatus -OperationStatusLink $importRequest.OperationStatusLink
[Console]::Write(".")
Start-Sleep -s 10
}
[Console]::WriteLine("")
$importStatus
For details, please refer to this ticket.
Curious if anyone has successfully done database scaffolding for EF Core with Oracle Cloud's Autonomous Database offering (not an on-premises Oracle DB). The tricky part seems to be the connection string. ADB requires now use of a wallet download, and I'm not sure how to handle that.
Oracle's ODP.Net example shows some special configuration commands to configure the data provider. But, these commands seem not to be available when using Scaffold-DbContext. The idea of placing the needed files in the same location as the data connection library sounds great, except NuGet libraries seem not to be persisted and are only deployed as part of the build process. Their blog post on connecting to ADB suggests rewriting the connection string to use the Easy Connect format which seems not to be supported.
They've an unrelated post on connecting SSRS to ADB that has some command line configuration for a data provider that I haven't tried yet because I'm not sure if it would work, but seems like it may potentially be useful at least in figuring out some sort of solution.
.NET Core/EF Core versions are 3.1.9.
A few variations I've tried...(with {} indicating placeholders for redacted information)
dotnet ef dbcontext scaffold "User Id={uname};Password={pwd};
Data Source={(description...) from tnsnames.ora}" Oracle.EntityFrameworkCore
result: request times out
dotnet ef dbcontext scaffold "User Id={uname};Password={pwd};
Data Source={tcps://... from blog}" Oracle.EntityFrameworkCore
result: invalid connection string
dotnet ef dbcontext scaffold "User Id={uname};Password={pwd};
Data Source={host}:{port}/{service_name}" Oracle.EntityFrameworkCore
result: Failed to connect to server
If anyone has any wisdom to share on this, I'd appreciate it.
Figured it out.
Copy wallet.sso, sqlnet.ora, and tnsnames.ora to your project root directory
Obtain the data source name from tnsnames.ora--you will typically see three options, one for {dbname}_high, one for {dbname}_low, one for {dbname}_medium. Refer to Oracle's documentation on the difference between them, but _high worked fine for me.
Execute your scaffolding command. Shown below is a basic example using dotnet ef dbcontext scaffold with a dbname of TEST, user id of ADMIN, and password of PASSWORD
dotnet ef dbcontext scaffold "Data Source=TEST_high;User Id=ADMIN;Password=PASSWORD" Oracle.EntityFrameworkCore
Probably need to modify OnConfiguring of the created DataContext...will update when I get a bit further.
There is an alternative to not use Wallets.
There is, in this link oracle autonomous database without wallet using TLS, a to-do to change Autonomous Database to use TLS insteado mTLS and wallets.
After changing the database, just use
dotnet ef dbcontext scaffold "Data Source=(description...);User Id=ADMIN;Password=PASSWORD" Oracle.EntityFrameworkCore
I can log in into SQL Server 2017 from SSMS with a SQL Server login. But when I'm debugging my Web API project, using EF Core, I get an exception
No process is on other end of pipe
The connection string is as below
"ConnectionStrings": {
"InventoryConnection": "Data Source=localhost;Initial Catalog=InventoryDB;user=aXXXXX;password=XXXXX"
}
I checked the SQL Server configuration manager and enabled TCP/IP and NamedPipes and SharedMemory and the protocol order list is SharedMemeory 1, TCP/IP 2 and Named Pipes 3. I tried all the solutions given at pinal dev But still the error persists.
I restarted the service, restarted my PC - but nothing works. Am I missing anything far out?
You should use 'User Id' instead of 'user' in your connection string.
Could you check this connection string?
Server=.;Database=InventoryDB;User Id=aXXXXX;Password=XXXXX;
For more information you can check this site:
https://www.connectionstrings.com/sql-server/
The error was because there was no database with the name that I specified in the connection string. I was expecting EF Core to create a new database. But turns out I was wrong.
I'm really mistaken at how EF Core differs from EF 6.x in terms of initial database creation. After posting the question, I stumbled on the point to create EF migrations to create a new database. I wondered aren't there other way to create an initial database when the application is first run(that's how EF 6.x supported). I came to know that Context.Database.EnsureCreated() achieves the same.
I was trying out a new console app with EF Core 5.0, and was hitting some errors similar to the above. Brand new DB entry in my LocalDB - and I had not installed the Microsoft.EntityFrameworkCore.Tools yet. Once I did that and did the Powershell NuGet Console commands
Add-Migration Init
Update-Database
The commands worked - and my connection was working as it was expected to do. I just had not remembered to do that - and that was my root cause.
I'm having problems publishing my MVC project.
I got the error "The model backing the 'ApplicationDbContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).", that was solved with the help of automigrations.
I published my project via ftp on the another machine, then added some changes on the model`s structure, run it on my computer, it launced succesfull and published again on the same another machine. When I tryed to run it there, an error occurred - "The model backing the 'ApplicationDbContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269)."
I'm using IIS7 on Windows 7.
To update database on another Db server you could make this way:
Configure connection string to use new server
Run Update-database command from package manager console, in some cases you will need to use -force flag. But be careful this could delete some your data. Also this command has - ConnectionStringName parameter this could help you to make updates.
This is a very bad approach and I don't recommend update database this way.
I would recommend you set up Initialzier for your context. I usually use MigrateDatabaseToLatestVersion and after changing your model you will need to add new migration, using Add-migration command.
To set up it use this command in application start method:
Database.SetInitializer<ApplicationDbContext>(new MigrateDatabaseToLatestVersion<ApplicationDbContext>());
You must update your database after changing the model. Another machine and your computer use the same database? If not you must update this database.