Managing the Intrinsic Database for LightSwitch
For the latest documentation on Visual Studio 2017, see Visual Studio 2017 Documentation.
If your LightSwitch application includes a database project in Visual Studio 2013, you can write scripts to manage the intrinsic database whenever you build or deploy that application. For example, you can populate a table with data or add an index for a non-unique field. When you design a LightSwitch application, you can perform some tasks, such as creating tables and editing data, by using the data designer, but other tasks can only be accomplished by adding SQL Server database project and writing a script. For more information about database-management tasks, see SQL Server Data Tools (SSDT).
You create scripts by using the Transact-SQL language, which is the standard for SQL Server development. See Transact-SQL Reference.
Database Projects and LightSwitch
When you add a database project to your LightSwitch solution, the contents of that project are incorporated into the intrinsic database that LightSwitch deploys when you build or deploy your application. But you must first associate the database project with the application by opening the Application Designer and then setting the SQL Server Database property. See How to: Add a Database Project.
You can add multiple database projects to a solution, but you can associate only one project at a time with the LightSwitch application. For example, you can write a different build script in a different database project for each environment to which you expect to deploy the application. You could then associate the appropriate project with the solution before each build.
If you create a post-deployment script, you can populate data, change a schema, or add an index after you deploy your application. See Walkthrough: Managing Data in a LightSwitch Application.
Because the script runs every time that you deploy the application, you must ensure that the script doesn't change the database in ways that you don't intend. For example, you can populate a table with data by using an INSERT statement, but you'll probably end up with multiple copies of the same data. Instead, you should avoid duplication by using a MERGE statement. See Inserting, Updating, and Deleting Data by Using MERGE.
A build script changes the intrinsic database while you deploy your application, instead of after you deploy it. Each build script is parsed and compared against the database schema to avoid potential errors, such as attempting to duplicate an index.