简体   繁体   English

如何在 Visual Studio 数据库项目 (SSDT) 上设置更改跟踪

[英]How to set change tracking on a Visual Studio database project (SSDT)

I have a SQL Server 2005 DB project and am looking to deploy the Schema over an existing DB that is on a later version of SQL Server.我有一个 SQL Server 2005 DB 项目,我希望在 SQL Server 更高版本上的现有数据库上部署架构。 The issue I have is that Change Tracking is enabled on the DB I wish to deploy to and so the first thing SSDT wants to do is disable CT.我遇到的问题是我希望部署到的数据库上启用了更改跟踪,因此 SSDT 要做的第一件事就是禁用 CT。 This poses a problem as I get the error below:这带来了一个问题,因为我收到以下错误:

(43,1): SQL72014: .Net SqlClient Data Provider: Msg 22115, Level 16, State 1, Line 5 Change tracking is enabled for one or more tables in database 'Test'. (43,1): SQL72014: .Net SqlClient Data Provider: Msg 22115, Level 16, State 1, Line 5 对数据库“Test”中的一个或多个表启用更改跟踪。 Disable change tracking on each table before disabling it for the database.在为数据库禁用更改跟踪之前禁用每个表的更改跟踪。 Use the sys.change_tracking_tables catalog view to obtain a list of tables for which change tracking is enabled.使用 sys.change_tracking_tables 目录视图获取启用更改跟踪的表的列表。 (39,0): SQL72045: Script execution error. (39,0): SQL72045: 脚本执行错误。 The executed script:执行的脚本:

 IF EXISTS (SELECT 1 FROM [master].[dbo].[sysdatabases] WHERE [name] = N'$(DatabaseName)') BEGIN ALTER DATABASE [$(DatabaseName)] SET CHANGE_TRACKING = OFF WITH ROLLBACK IMMEDIATE; END

In an effort to get around this I have created a PreDeployment script that executes the below:为了解决这个问题,我创建了一个执行以下内容的 PreDeployment 脚本:

/* Run pre-deployment scripts to resolve issues */
IF(SELECT SUBSTRING(@@VERSION, 29,4)) = '11.0'

BEGIN

PRINT 'Enabling Change Tracking';

DECLARE @dbname VARCHAR(250)
SELECT @dbname = DB_NAME()

EXEC('
        IF NOT EXISTS(SELECT * FROM [master].[dbo].[sysdatabases] WHERE name = ''' + @dbname + ''')
        
        ALTER DATABASE ['+ @dbname +
        ']SET CHANGE_TRACKING = ON
        (CHANGE_RETENTION = 5 DAYS, AUTO_CLEANUP = ON);
');

EXEC('
IF NOT EXISTS(SELECT * FROM sys.change_tracking_tables ctt
          INNER JOIN sys.tables t ON t.object_id = ctt.object_id
          INNER JOIN sys.schemas s ON s.schema_id = t.schema_id
          WHERE t.name = ''TableName'')
BEGIN
    ALTER TABLE [dbo].[TableName] ENABLE CHANGE_TRACKING;
END;');

So based on the DB Version Change Tracking is set to enabled on the DB and relevant Tables assuming it is not already enabled.I got this idea from a previous post: # ifdef type conditional compilation in T-SQL sql server 2008 2005因此,基于 DB Version Change Tracking 设置为在 DB 和相关表上启用,假设它尚未启用。我从以前的帖子中得到了这个想法: # ifdef type conditional compiler in T-SQL sql server 2008 2005

Unfortunately this is still not working as SSDT is trying to disable Change Tracking before the PreDeployment script is executed.不幸的是,这仍然不起作用,因为 SSDT 试图在执行 PreDeployment 脚本之前禁用更改跟踪。

Make sure change tracking is enabled in your database project.确保在您的数据库项目中启用了更改跟踪。

Open your database project's properties > Project Settings > Database Settings... > Operational tab > check the "Change tracking" option打开数据库项目的属性 > 项目设置 > 数据库设置... > 操作选项卡 > 检查“更改跟踪”选项

As Keith said if you want it in enable it.正如基思所说,如果你想启用它。 If you do want to disable it then just run your script before doing the compare so you have a pre-pre-deploy script like:如果您确实想禁用它,那么只需在进行比较之前运行您的脚本,这样您就有一个预部署脚本,例如:

https://the.agilesql.club/Blog/Ed-Elliott/Pre-Compare-and-Pre-Deploy-Scripts-In-SSDT https://the.agilesql.club/Blog/Ed-Elliott/Pre-Compare-and-Pre-Deploy-Scripts-In-SSDT

If you are disabling it then it is a one off thing so pretty simple.如果您禁用它,那么这是一件非常简单的事情。

Other options are to write your own deployment contributor and raising a bug via connect.其他选项是编写自己的部署贡献者并通过连接提出错误。

Deployment Contributor:部署贡献者:

https://the.agilesql.club/blog/Ed-Elliott/2015/09/23/Inside-A-SSDT-Deployment-Contributor https://the.agilesql.club/blog/Ed-Elliott/2015/09/23/Inside-A-SSDT-Deployment-Contributor

https://github.com/DacFxDeploymentContributors/Contributors https://github.com/DacFxDeploymentContributors/Contributors

Ed埃德

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Visual Studio SSDT 数据库项目在重新启动时不会显示架构节点 - Visual Studio SSDT Database project will not display schema node on restart 如何在连接到数据库的Visual Studio 2017中设置SQL数据库项目? - How to set the SQL Database Project in the Visual Studio 2017 connected to the Database? 在Visual Studio中使用报表服务器项目向导(SSDT)对每个数据库运行查询 - Using Report Server Project Wizard (SSDT) in Visual Studio to run a query against each database Visual Studio 2019 - SSDT - Visual Studio 2019 - SSDT 带有 SSDT 版本的 Visual Studio - Visual Studio with SSDT version Visual Studio SSDT数据比较如何比较单个数据库中的两个表 - Visual Studio SSDT Data Compare how to compare two tables in a single database 如何从现有数据库创建SSDT项目? - How to create SSDT project from existing database? Visual Studio 2019 SSDT 项目 SQLCLR 签名未生成签名的 DLL - Visual Studio 2019 SSDT project SQLCLR signing not generating signed DLL 如何在 Visual Studio 中设置 SQL 数据库项目中的数据库连接字符串? - How to have database connection string in the SQL Database Project set up in the Visual Studio? 如何命名数据库的文件名并在Visual Studio 2015数据库项目中设置其位置? - How to name the filenames of a database and set its location in Visual Studio 2015 Database project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM