dotnet ef does not have db clone commands.
Rather than creating script, you can clone/copy database using SSMS. Then update connection string in asp.net application, test and deply.
Here is the documentation of MS SQL server.
https://learn.microsoft.com/en-us/sql/relational-databases/databases/use-the-copy-database-wizard?view=sql-server-ver15
Any database have the copy/clone feature.
And if you use EF Code First, Please check ef migrations for existing database. https://learn.microsoft.com/en-us/ef/ef6/modeling/code-first/migrations/existing-database
If you need to copy db only using application, you can create stored procedure and execute from application. here is the details.
- The source database is backed up to a file using dynamic T-SQL execution
- The backup is then restored to a new database, using the RESTORE DATABASE WITH MOVE OPTION again using dynamic T-SQL
The procedure takes 6 parameters (all parameters should be supplied):
- Source database name
- Source database logical data file name
- Source database logical log file name
- Backup file name and path
- Destination database name
- Destination database folder location for database files
Here is the T-SQL procedure source code:
USE master;
GO
CREATE PROCEDURE dbo.CloneDBUsingBackupRestore (
@SourceDatabaseName VARCHAR(200)
,@SourceDatafileLogicalName VARCHAR(200)
,@SourceLogfileLogicalName VARCHAR(200)
,@BackupFile VARCHAR(2000)
,@TargetDatabaseName VARCHAR(200)
,@TargetDatabaseFolder VARCHAR(2000) )
AS
BEGIN
SET NOCOUNT ON
DECLARE @tsql VARCHAR(2000)
DECLARE @TargetDataFile VARCHAR(2000)
DECLARE @TargetLogFile VARCHAR(2000)
SET @TargetDataFile = @TargetDatabaseFolder + @TargetDatabaseName + '.mdf';
SET @TargetLogFile = @TargetDatabaseFolder + @TargetDatabaseName + '.ldf';
-- Backup the Source Database
IF @SourceDatabaseName IS NOT NULL
BEGIN
SET @tsql = CONCAT ( 'BACKUP DATABASE ' ,@SourceDatabaseName
,' TO DISK = ' ,QUOTENAME(@BackupFile, '''') )
PRINT 'Executing query : ' + @tsql;
EXEC (@tsql)
END
-- Restore database from the Backup File into target Data File and target Log File
SET @tsql = CONCAT ( 'RESTORE DATABASE ' ,@TargetDatabaseName
,' FROM DISK = '
,QUOTENAME(@BackupFile, '''')
,' WITH MOVE '
,QUOTENAME(@SourceDatafileLogicalName, '''') ,
' TO ' ,QUOTENAME(@TargetDataFile, '''') ,'
, MOVE ' ,QUOTENAME(@SourceLogfileLogicalName, '''')
,' TO ' ,QUOTENAME(@TargetLogFile, '''') )
PRINT 'Executing query : ' + @tsql EXEC (@tsql)
SET NOCOUNT OFF
END GO
Call Using ASP.Net Code.
using (SqlConnection con = new SqlConnection(connString))
{
using (SqlCommand cmd = new SqlCommand("dbo.CloneDBUsingBackupRestore", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@SourceDatabaseName", SqlDbType.VarChar).Value
="Northwind";
cmd.Parameters.Add("@SourceDatafileLogicalName ", SqlDbType.VarChar).Value
="Northwind_Log";
cmd.Parameters.Add("@BackupFile", SqlDbType.VarChar).Value
="C:\SQL\nw.bak";
cmd.Parameters.Add("@TargetDatabaseName", SqlDbType.VarChar).Value
="NorthwindClone";
cmd.Parameters.Add("@TargetDatabaseFolder ", SqlDbType.VarChar).Value
="C:\SQL";
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}