SQL数据库项目:根据构建配置构建不同的脚本 [英] SQL Database Project: build different scripts depending on build configuration

查看:139
本文介绍了SQL数据库项目:根据构建配置构建不同的脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要解决的问题是根据构建配置来构建不同的脚本.

The problem I want to solve is to build different scripts depending on build configuration.

假设我们有两个SQL Server实例:

Say we have two instances of SQL Server:

  • 具有连接的链接服务器的企业版
  • 用于脱机开发和单元测试的LocalDb版本

当LocalDB用本地表替换那些视图时,企业版具有链接服务器的视图.

Enterprise version has views for Linked servers when LocalDB substitues those views with local tables.

那些链接服务器视图和本地表具有相同的名称和一组字段. 因此,默认情况下它们不包括在构建中(构建操作=无"). 而是将它们包含在项目文件的BeforeBuild Target中.

Those Linked Server views and local tables have the same names and set of fields. So they are not included in build by default (Build Action = None). Instead they are included in build in BeforeBuild Target of the project file.

<Target Name="BeforeBuild">

    <ItemGroup Condition=" '$(Configuration)' == 'LocalDb'">
        <Build Include="Local_Tables\*.sql" />
    </ItemGroup> 

    <ItemGroup Condition=" '$(Configuration)' != 'LocalDb' ">
        <Build Include="Linked_Server_Views\*.sql" />
    </ItemGroup>

</Target>

但是问题是Visual Studio会缓存数据库模型,如果我们先为LocalDb构建项目,然后再尝试为Enterprise配置构建项目-Visual Studio输出错误:

But the problem is that Visual Studio caches the DB Model and if we first build project for LocalDb and then try build project for Enterprise configuration - Visual Studio outputs errors:

错误:SQL71508:模型已经具有一个具有相同名称的元素

如果要关闭和打开解决方案或卸载项目并重新加载项目",Visual Studio将重新创建dbmdl文件,并且正在构建企业配置而不会出现错误.

If to close and open solution or Unload Project and Reload Project, Visual Studio recreates dbmdl files and Enterprise configuration is being build without errors.

所以我的假设是,如果刷新dbmdl缓存,我将获得顺畅的构建而不会出错.

So my assumption is if I refresh dbmdl cache I will get smooth build without error.

在Visual Studio 2012中打开或重新加载SQL Server数据库项目时,它将创建扩展名为 dbmdl 的文件,该文件是反序列化和缓存的db模型,如

When you open or reload SQL Server database project in Visual Studio 2012, it creates a file with extension dbmdl, which is a deserialized and cached db model as described here.

在重新创建dbmdl文件时,Visual Studio输出以下内容:

In the moment of the dbmdl file recreation Visual Studio outputs the following:

Deserializing the project state for project 'MyProject.sqlproj'...
Detecting file changes for project 'MyProject.sqlproj'...
Deserialization has been completed for project 'MyProject.sqlproj'.

如何在不重新加载项目且不更改项目xml文件的情况下强制Visual Studio刷新dbmdl缓存?

How to force Visual Studio to refresh the dbmdl cache without project reload and without changing of the project xml file?

是否有一种刷新dbmdl缓存的方法,可将命令放置到项目xml文件的BeforeBuild或AfterBuild目标中?

Is there a way to refresh the the dbmdl cache placing a command into BeforeBuild or AfterBuild targets of the project xml file?

还是解决问题的整个方法是错误的,还有另一种方法可以根据构建配置来构建不同的脚本?

Or the whole approach to the problem is wrong and there is another way to build different scripts depending on build configuration?

推荐答案

在使用复合项目时,您可能还有另一个选择.杰米·汤普森(Jamie Thompson)在这里发表了有关他们的博客: http://sqlblog.com/blogs/jamie_thomson/archive/2013/03/10/deployment-of-client-specific-database-code-using-ssdt.aspx

You may have another possible option in using composite projects. Jamie Thompson blogged about them here: http://sqlblog.com/blogs/jamie_thomson/archive/2013/03/10/deployment-of-client-specific-database-code-using-ssdt.aspx

这将使您可以构建主项目,并使用特定于环境的代码来捆绑其他项目.您可以通过在发行脚本中进行一些检查来部署适当的服务器.

This would let you build a main project and tie in additional projects with the environment-specific code. You could deploy the appropriate one by doing some checks in the release scripts.

这篇关于SQL数据库项目:根据构建配置构建不同的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆