Black Friday is here! Get up to 30% off on Devart products!
ends in   {{days}}
Days
{{timeFormat.hours}}
:
{{timeFormat.minutes}}
:
{{timeFormat.seconds}}

Tutorial: How to Connect Visual Studio LightSwitch to PostgreSQL with dotConnect for PostgreSQL Data Provider

Microsoft Visual Studio LightSwitch, first released in 2011, quickly became popular as it allows quick and flexible development of busyness data-oriented applications.

As Devart always strives to support the newest cutting-edge-data oriented technologies in its products, our dotConnect ADO.NET providers supported Visual Studio LightSwitch since its first beta.

This Microsoft Visual Studio LightSwitch tutorial helps you to quickly get started with creating LightSwitch apps connecting to PostgreSQL with dotConnect for PostgreSQL ADO.NET provider. To complete this tutorial, you will need to install Visual Studio LightSwitch and dotConnect for PostgreSQL Trial or Professional edition.

Download Now dotConnect for PostgreSQL

Create An Application

  1. Create a new LightSwitch project. To do this, select File -> New Application from the Visual Studio menu. In the displayed New Project dialog box under Installed Templates select LightSwitch and then select a project template for the preferred programming language and click OK.

    dotConnect for PostgreSQL - LightSwitch empty designer
  2. Click Attach to external Data Source and select Database in the displayed dialog box. Click the Next button.

    dotConnect for PostgreSQL - Attach Data Source Wizard
  3. Select PostgreSQL in the Data source list, select dotConnect for PostgreSQL in the Data provider drop-down list, and click the Continue button.

    dotConnect for PostgreSQL - Choose Data Source dialog box
  4. Specify connection settings and click OK.

    dotConnect for PostgreSQL - LightSwitch PostgreSQL connection properties
  5. Select a table or view to work with data from, specify the DataSource name if you need, and click Finish. In our tutorial we have selected the Dept table. Now you have created a new external data source.

    dotConnect for PostgreSQL - Created LightSwitch data source
  6. Create a new screen for this data source by clicking the Screen button (highlighted on the previous screenshot) or press CTRL+SHIFT+E. Select Editable Grid Screen in the Select a screen template list, and then select the newly created data source under Screen Data. Then click OK.

    dotConnect for PostgreSQL - Add New Screen dialog box
  7. Now a simple LightSwitch application with PostgreSQL connection is ready. However it throws an exception when you try to update data because of the unsupported default LightSwitch transaction isolation level RepeatedRead.

    dotConnect for PostgreSQL - screen

Avoid Exception

  1. Switch from Logical View to File View in Solution Explorer.
  2. Add reference to System.Transactions to the Server project.
  3. Return to the Logical View.
  4. Right-click the DataSource and select View Code from the shortcut menu.
  5. Add the following line to the using statements in the beginning of the file:

    using System.Transactions;
  6. Add the following code to the partial class:

    private TransactionScope _tscope;
    
    partial void SaveChanges_Executing()
    {
      _tscope = new TransactionScope(TransactionScopeOption.Required,
        new TransactionOptions
          {
            IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted
          });
    }
    
    partial void SaveChanges_Executed()
    {
      _tscope.Complete();
      _tscope.Dispose();
    }
  7. Now our LightSwitch application is ready and you can run it.

    dotConnect for PostgreSQL - LightSwitch application

Deployment of LightSwitch applications, using dotConnect for PostgreSQL, has the same requirements as deployment of Entity Framework applications, using dotConnect for PostgreSQL. LightSwitch specific requirements to deployment are described here. dotConnect for PostgreSQL specific requirements to deployment are described here.

LightSwitch Limitations

As the LightSwitch technology was designed with Microsoft SQL Server in mind, support for Visual Studio LightSwitch in PostgreSQL (and other) data providers has some certain limitations. By default, LightSwitch uses transactions with isolation level RepeatableRead, which are not supported by PostgreSQL (and most other databases). dotConnect for PostgreSQL throws an exception when LightSwitch application tries to update data. In this tutorial we provide the steps to avoid this issue (see above).

Another limitation is that you cannot import database objects from a schema containing two tables that have several relations between them, and one of these relations is a many-to-many association via an juction table. Attach Data Source Wizard cannot create a valid model in such case and produces errors when it comes to choosing database objects to import. Unfortunately, the only solution is to connect as a user that doesn't have enough privileges to see the juction table (or at least one of the two table with many-to-many association).


Back to list

dotConnect for PostgreSQL

Get enhanced ORM-enabled data provider for PostgreSQL and develop .NET applications working with PostgreSQL data quickly and easily!