You can consume your backend ODataVisual Studio LightSwitch services in AngularJs. Coding your business layer in LightSwitch will save you a lot of development time and reduce the code you will be required to write.

This example uses JayData, a unified data access library for JavaScript that will be used to allow AngularJs to read and write to the LightSwitchOData layer.

The Application

Tasks are displayed in a list.

Clicking the add new button will display the form to allow a new task to be created.

The task can be entered and saved.

New tasks will appear immediately.

Clicking on an existing task will open it in a form so that it can be edited.

Clicking the remove button deletes a task.

Business rules created in the LightSwitch business layer are enforced.

Create the Entity Context

JayData tracks the changes of the entities using an entity context. Adding, updating, deleting entities happen through the context, which holds the reference to the added, updated and deleted objects and dispatches the operations (HTTP requests) to the OData endpoint after calling context.saveChanges(). Entity contexts can be defined manually or generated the JaySvcUtil.exe. You can download the latest version from JaySvcUtil CodePlex page.

When we run the application we note the url.

If we change the path to ApplicationData.svc we see the OData service.

We use the JayScvUtil tool to create the entity context using the format:

Michael - Thanks for the post. If I can get my custom code up and running it should be a real time saver. I'm able to compile and run your sample project without any problems. When I try to use this in a project I've started from scratch, anytime I reference a property of an object on my scope, I get a quoted string of all the values of the properties in that object. For example, I have object that looks like:

I get:
W/"3,0,'Customer',datetime'2011-03-29T00%3A00%3A00',datetime'2012-03-12T00%3A00%3A00',null,datetime'2011-01-01T00%3A00%3A00',datetime'2011-12-01T00%3A00%3A00',datetime'2012-01-01T00%3A00%3A00',null,null,null,1,null,null,null,null,3,0,0,1"

You ran the JaySvcUtil.exe tool, right? Look at the output of that file (ApplicationData.js) and see if it looks like mine but with the obvious changes that reflect your tables. If it does not look correct ensure you are running the tool correctly (using JaySvcUtil.exe -m http://{domain}:{port}/ApplicationData.svc/$metadata -o ApplicationData.js). If the ApplicationData.js file looks correct, then post the code form your Angular Controller.

Sorry, but I'm finally getting back to looking at this again. To answer your question, yes, I am using the JaySvcUtil against my LightSwitch service. Since writing my original post, I stepped back and simplified everything with the hope of finding the issue, but I still get the same results. I'll post the code, starting from the beginning.

To browse LightSwitch service, I point the browser to http://localhost:32156/ETLCoreData.svc/ , and I see the following XML:

Couldn't be more simple, right? But the output I get on the page is literally, for each server item, I get a bullet with a string containing all the properties of the server object as follows:
* W/"null,'sqlETL','SQLETL','Provider%3Dsqloledb%3BData%20Source%3Dsqletl%...<shortened for brevity>...',1,0,0"
* W/"null,'sqletldev%5Csqletly','SQLETLy','Provider%3Dsqloledb%3BData%...<shortened for brevity>...',1,0,0"

Surely it is something simple that I've overlooked somewhere. Any thoughts or recommendations are very much appreciated.

Yes, but that service is not using the intrinsic db. I created ETLCoreData as a data source that references an external SQL2K5 DB. (Right click on "Data Sources" in the Server project, select "Add Data Source...". Then in the wizard, I select Database -> Next -> [Fill in connection properties] -> OK. Then I choose my table named "Server" from the tree and specify the name of the data source as "ETLCoreData" and click Finish.

I am sorry but I never tried to connect that way. The only thing I can think of is that perhaps it doesn't work with a SQL 2005 data source, but I do not know. The http://jaydata.org/[^] site may be able to help.

I came across a similar issue with errors similar to yours. My problem turned out to be the version of sql I was using was too old for lightswitch. I switched to a SQL 2012 database and the issue was resolved.

The problem I discovered was the sequence object implemented in SQL 2012+. Some mechanism of Lightswitch apeared to be dependent on this SQL sequence object. None of the initial errors I received indicated this was the issue, but after digging into it more I discovered that was indeed the problem.

I may be off base, but thought it was worth posting this as I saw you mentioned you were trying to set up your Lightswitch project with a SQL 2005 DB.