数据层应用程序 - 部署后

发布于 2024-10-19 13:06:31 字数 1244 浏览 5 评论 0原文

这是一件如此简单的事情,即使在这里问也让我觉得很愚蠢,但由于我已经坚持了很长时间,所以我会在这里问。我正在 Visual Studio 中开发数据层应用程序。我有一些常用的东西,比如表、存储过程和一些部署后数据。默认情况下,数据层应用程序附带 Scripts/Post-Deployment 文件夹。该文件夹内有一个名为 Script.PostDeployment.sql 的文件。为了更有条理,我在部署后创建文件夹作为 StaticData 和 TestData。我用于创建数据的插入语句位于这些文件夹内。因此,基于这个结构,我将以下代码添加到我的 Script.PostDeployment.sql 中:

    /*
Post-Deployment Script Template                         
--------------------------------------------------------------------------------------
 This file contains SQL statements that will be appended to the build script.       
 Use SQLCMD syntax to include a file in the post-deployment script.         
 Example:      :r .\myfile.sql                              
 Use SQLCMD syntax to reference a variable in the post-deployment script.       
 Example:      :setvar TableName MyTable                            
               SELECT * FROM [$(TableName)]                 
--------------------------------------------------------------------------------------
*/

:r .\StaticData\States.sql
:r .\TestData\Logins.sql

问题是上面的代码不起作用。由于某些原因,部署命令只是忽略路径并在 Scripts/Post-Deployment 中查找 States.sql 和 Logins.sql,而不是在适当的子文件夹中查找。还有其他人遇到过类似的事情吗?非常简单的问题,但花了我很长时间才解决这个问题。我已尽力解释,但提出问题,我可以尽力使事情变得更清楚。 谢谢!

This is such a simple thing that even asking here is making me feel stupid but since I have been stuck on this for long time, I will ask it here. I am working on a data-tier application in visual studio. I have usual things like tables, stored procs and some post deployment data. By default, data tier application comes with Scripts/Post-Deployment folder. Inside this folder there is a file called Script.PostDeployment.sql. Just to be little more organised, I am creating folders inside Post-Deployment as StaticData and TestData. My insert statements for data creating are locatied inside these folders. So, based on this structure, I am adding following code to my Script.PostDeployment.sql:

    /*
Post-Deployment Script Template                         
--------------------------------------------------------------------------------------
 This file contains SQL statements that will be appended to the build script.       
 Use SQLCMD syntax to include a file in the post-deployment script.         
 Example:      :r .\myfile.sql                              
 Use SQLCMD syntax to reference a variable in the post-deployment script.       
 Example:      :setvar TableName MyTable                            
               SELECT * FROM [$(TableName)]                 
--------------------------------------------------------------------------------------
*/

:r .\StaticData\States.sql
:r .\TestData\Logins.sql

The problem is the above code does not work. For some starnge reason, the deploy command just ignores the paths and looks for States.sql and Logins.sql in Scripts/Post-Deployment and not in appropriate subfolders. Anyone else encountered anything similar? Very simple issue, but taking me forever to get around this. I have tried my best to explain, but ask questions and I can try to make things clearer.
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

高跟鞋的旋律 2024-10-26 13:06:31

我看了你的示例代码。当我尝试重现此问题时,我在 Visual Studio 2010 中使用 SQL 2008 数据库项目,但您的项目是数据层应用程序,这是非常不同的;当我切换到使用数据层应用程序时,我能够重现您所看到的内容。

数据层应用程序生成 DAC 包,其中包含对象的定义,还包含用户定义的脚本,例如部署前和部署后脚本。现在,我不是 100% 确定(我以前没有使用过 DAC 包,所以我基于观察和研究),但我猜测 DAC 包的文件结构不支持子- Script\Post-deployment 文件夹下的文件夹;我假设它内部有一个非常严格的文件夹结构。因此,DACCompiler 似乎被设计为仅从部署后脚本中的文件引用中删除文件名,并且忽略目录路径。

有一份关于数据层应用程序的白皮书 此处。其中有一个关于向包添加部署后脚本的部分,该部分提供了一些最佳实践,包括以下内容:

• 当您在解决方案资源管理器中工作时,建议您将所有部署后命令包含在Script.PostDeployment.sql 脚本文件。这是因为 DAC 包中仅包含一个部署后文件。换句话说,您不应该创建多个文件。

现在,从技术上讲,这就是 :r 命令的作用,但您可能会发现手动将命令直接嵌入到文件中更容易。

这也可能只是 DACCompiler 设计中的一个错误。

以下是我建议您做的事情:

  • 目前,最简单的事情 - 我
    相信 - 只是移动脚本
    直接在部署后
    文件夹;赋予它们独特的、描述性的
    名字来弥补没有
    子目录。
  • 或者,如果你真的想要
    保留子目录,添加一个
    为您的项目预构建命令;
    让它复制脚本
    子目录进入
    之前的部署后目录
    构建开始(您需要确保
    脚本具有唯一的文件名)
  • 如果您认为这是一个错误,或者
    应该存在的功能,请转到
    http://connect.microsoft.com/SQLServer
    并建议产品团队
    在未来版本中解决这个问题
    产品。这是一个很棒的地方
    提出此类建议,
    因为反馈会发送到
    产品团队、用户社区
    大可以对反馈进行投票
    增加其重量,并且产品
    团队可以通过以下方式与您联系
    有关反馈的信息。

当然,你可以坚持看看其他人是否有不同的答案,如果有,那就太好了!但我猜如果还没有其他人做出回应,那么可能就没有人做出回应;我在挖掘中当然找不到任何东西。

我希望这些信息总体上有所帮助。我希望我能为您提供一种让它立即运行的方法,但我认为您最好的选择是在当前设计的限制范围内工作并向 Connect 发布反馈。

祝你好运。

I took a look at your sample code. When I had tried to reproduce this, I was using a SQL 2008 database project in Visual Studio 2010, but what your project is a data-tier application, and that is very different; when I switched to using a data-tier application, I was able to reproduce what you're seeing.

Data-tier applications produce DAC packages that contain the definitions of objects and also contain user-defined scripts, like the pre and post deployment scripts. Now, I'm not 100% certain (I haven't used DAC packages before, so I'm basing this on observation and research), but I'm guessing that the file structure of the DAC package doesn't support sub-folders under the Script\Post-deployment folder; I am assuming it has a pretty strict folder structure internally. Consequently, the DACCompiler appears designed to strip out just the filenames from your file references in the post-deployment script, and it ignores the directory path.

There is a whitepaper on data-tier applications here. In it is a section on adding a post-deployment script to the package, and in that section are some best practices, including the following:

• When you work in Solution Explorer, it is recommended that you include all post-deployment commands in the Script.PostDeployment.sql script file. This is because only one post-deployment file is included in the DAC package. In other words, you should not create multiple files.

Now, technically, that's what the :r command does, but you may find it easier to just embed the commands directly into the file manually.

It's also possible that this is simply a bug in the design of the DACCompiler.

Here's what I recommend that you do:

  • For now, the easiest thing to do - I
    believe - is just to move the scripts
    up directly under the Post-Deployment
    folder; give them unique, descriptive
    names to compensate for not having
    the subdirectories.
  • Alternatively, if you really want to
    keep the subdirectories, add a
    pre-build command to your project;
    have it copy the scripts from the
    subdirectories into the
    post-deployment directory before the
    build starts (you'll need to ensure
    the scripts have unique filenames)
  • If you feel that this is a bug, or a
    feature that should exist, go to
    http://connect.microsoft.com/SQLServer
    and recommend that the product team
    address it in a future version of the
    product. This is a great place to
    make these kinds of recommendations,
    because the feedback goes to the
    product team, the user community at
    large can vote on feedback to
    increase its weight, and the product
    team can communicate back to you with
    information about the feedback.

And, of course, you could hold out and see if somebody else has a different answer, and if there is, great! But I'm guessing if nobody else has responded yet, then probably there isn't one; I certainly couldn't find anything in my digging.

I hope overall this information is helpful. I wish I could give you a way to have it work now, but I think your best bet is to work within the limitations of the current design and post feedback to Connect.

Good luck.

静水深流 2024-10-26 13:06:31

我有一种感觉,这对于解决您的问题来说为时已晚,但可能值得一看。 dacpac 格式只是一个 zip 文件,其中包含一系列 xml 文件和 SQL 脚本。如果您将文件的扩展名更改为 zip,那么您将能够访问其中包含的文件。 postdeploy.sql 文件应包含部署后脚本及其引用的任何其他脚本的聚合。

I have a feeling that this will be too late in the pipeline to help with your problem but it might be worth a look. The dacpac format is just a zip file that contains a series of xml files and SQL scripts. If you change the extension of the file to zip then you will be able to access the files that it contains. The postdeploy.sql file should contain the aggregation of your post deployment script and any others that it references.

幻想少年梦 2024-10-26 13:06:31

我刚刚使用 Visual Studio 2013 尝试过,它有效。

    IF ( '$(DeployType)' = 'Qualification' ) 
    BEGIN --Run scripts 
        PRINT 'Deploying Qualification Specific scripts.'     
        :r .\Qualification\"QualificationSpecificTestScript.sql"
    END 
    ELSE IF ( '$(DeployType)' = 'Production' ) 
    BEGIN --Run scripts 
        PRINT 'Deploying Production Specific scripts.' 
        :r .\Production\"ProductionSpecificTestScript.sql"
    END

内容 QualificationSpecificTestScript.sqlProductionSpecificScript.sql 将插入到生成的部署后脚本中。

这是生成的脚本文件(只是相关部分):

     IF ( '$(DeployType)' = 'Qualification' ) 
     BEGIN --Run scripts 
        PRINT 'Deploying Qualification Specific scripts.'     
            begin transaction;
            PRINT 'IN QUALIFICATION ENVIRONMENT POST DEPLOYMENT SCRIPT'
            commit transaction;
     END
     ELSE IF ( '$(DeployType)' = 'Production' ) 
     BEGIN --Run scripts 
        PRINT 'Deploying Production Specific scripts.' 
            begin transaction;
            PRINT 'IN PRODUCTION ENVIRONMENT POST DEPLOYMENT SCRIPT'
            -- TODO:  Confirm this record should be deleted
            --DELETE TB_VariableName where Id = 9514
            commit transaction;
     END

I just tried this using Visual Studio 2013 and it works.

    IF ( '$(DeployType)' = 'Qualification' ) 
    BEGIN --Run scripts 
        PRINT 'Deploying Qualification Specific scripts.'     
        :r .\Qualification\"QualificationSpecificTestScript.sql"
    END 
    ELSE IF ( '$(DeployType)' = 'Production' ) 
    BEGIN --Run scripts 
        PRINT 'Deploying Production Specific scripts.' 
        :r .\Production\"ProductionSpecificTestScript.sql"
    END

The contents QualificationSpecificTestScript.sql and ProductionSpecificScript.sql are inserted into the generated Post Deployment script.

Here is the generated script file (just the relevant section):

     IF ( '$(DeployType)' = 'Qualification' ) 
     BEGIN --Run scripts 
        PRINT 'Deploying Qualification Specific scripts.'     
            begin transaction;
            PRINT 'IN QUALIFICATION ENVIRONMENT POST DEPLOYMENT SCRIPT'
            commit transaction;
     END
     ELSE IF ( '$(DeployType)' = 'Production' ) 
     BEGIN --Run scripts 
        PRINT 'Deploying Production Specific scripts.' 
            begin transaction;
            PRINT 'IN PRODUCTION ENVIRONMENT POST DEPLOYMENT SCRIPT'
            -- TODO:  Confirm this record should be deleted
            --DELETE TB_VariableName where Id = 9514
            commit transaction;
     END
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文