Create LocalDB for testing from Visual Studio SQL project

限于喜欢 提交于 2020-01-01 09:40:50

问题


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:

  1. Create LocalDB
  2. Run some Pre-scripts(to add test data)
  3. Run test
  4. Run some Post-scripts (if needed to run other test on this database)
  5. Remove 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?

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. Related question is How to disable predeployment and postdeployment scripts in DacServices.Deploy()


回答1:


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”


来源:https://stackoverflow.com/questions/42962711/create-localdb-for-testing-from-visual-studio-sql-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!