创建LocalDB以从Visual Studio SQL项目进行测试 [英] Create LocalDB for testing from Visual Studio SQL project
问题描述
我正在尝试为我的项目创建集成测试.我需要测试一个通过存储库调用存储过程的控制器.应该在一定范围的测试的每次运行中创建一个空数据库.因此,我将执行以下步骤:
I'm trying to create an integration tests for my project. I need to test a controller that makes call to stored procedure through repository. An empty database should be created on each runs of certain scope of tests. So I'm going to implement the following steps:
- 创建 LocalDB
- 运行一些脚本(添加测试数据)
- 运行测试
- 运行一些脚本(如果需要在此数据库上运行其他测试)
- 删除 LocalDB
- Create LocalDB
- Run some Pre-scripts(to add test data)
- Run test
- Run some Post-scripts (if needed to run other test on this database)
- Remove LocalDB
在我的解决方案中,我的所有表和SP都为.sqlproj
.如何使用C#代码创建与.sqlproj
中的结构相同的 LocalDB ?或者如何生成包含所有对象的脚本以在 LocalDB 上运行它?
In my solution I have .sqlproj
with all tables and SPs. How can I create LocalDB with the same structure as in .sqlproj
from C# code? Or how can I generate the script with all objects to run it on LocalDB?
.sqlproj
的bin文件夹中存在 MyProjectName_Create.sql ,但它不在cmd.ExecuteNonQuery();
In bin folder of .sqlproj
exists MyProjectName_Create.sql but it doesn't run in cmd.ExecuteNonQuery();
任何帮助/建议将不胜感激.谢谢
Any help/suggestion will be appreciated. Thanks
P.S.相关问题是如何在DacServices中禁用预部署和后部署脚本.Deploy()
P.S. Related question is How to disable predeployment and postdeployment scripts in DacServices.Deploy()
推荐答案
在sqlproj的build文件夹中,您还可以找到[DBName] .dacpac文件.您可以使用sqlpackage.exe将dacpac部署到您的localdb. Ben Day在
In the build folder of your sqlproj you can also find [DBName].dacpac file. You can use sqlpackage.exe to deploy the dacpac to your localdb. Ben Day explains in this blog pretty nicely. You can find sqlpackage.exe in your sql server installation folder if it is not directly available from the command line.
SqlPackage /Action:Publish
/SourceFile:"Benday.SampleApp.Database.dacpac"
/TargetDatabaseName:BendaySampleAppDb
/TargetServerName:"demo2012util"
这篇关于创建LocalDB以从Visual Studio SQL项目进行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!