5
votes

I have a WPF application that uses LINQ-to-SQL on a local .MDF file. This solution is simple, easy, and effective, i.e. I set up my model once, then read/write data anywhere via LINQ:

        using (var db = Datasource.GetContext())
        {
            oldItem = (from i in db.Infos
                       where i.Id == TheId
                       select i).SingleOrDefault();
            CreateForm(db, FormBase, oldItem, Button_Save);
        }

What is the dead-simple equivalent of this for Silverlight apps?

Searching I find an explosion of terms:

  • WCF RIA Services, WCF Data Services (ADO.NET Data Services, Astoria), Data Services Toolkit
  • .NET RIA Services
  • OData(Dallas)
  • GData
  • REST, REST-based, REST-like, REST-inspired
  • XML, JSON, RDF+XML
  • web services, SOA
  • cloud-based services, Azure, SQL Azure, Azure Services Platform

All I want to do is this:

  • create an .mdf file
  • use some LINQ-to-SQL-like tool to generate a web-enabled (REST?) data layer etc.
  • ftp the .mdf file and classes up to my ASP.NET web hosting service
  • write silverlight clients that read and write to this data source with LINQ
5

5 Answers

4
votes

Concentrate on learning RIA Data Services, or WCF Data Services. It converts your LINQ queries inside Silverlight to REST requests, and saves you from writing some of the infrastructure code. The whole idea is that your SL app communicates only to web services, you don't have access to physical DB, like when you're using some ORM (L2S). In SL, your are inside browser's sandbox, which prevents you from accessing file system, including db files.
Other approach is to write web service and expose data through it (GetArticleByID), and then you consume that services from Silverlight. Then you use LINQ to iterate on fetched, loaded data.

3
votes

Here's what to do -

  1. Create a Silverlight app using Visual Studio. You will get two projects, one with the Silverlight XAML, and the other a web application to host it.

  2. In the web application, add your DBML (Linq-2-SQL) file. Configure as normal

  3. In the web application, add a Silverlight enabled WCF service

  4. In the WCF service, define some methods that access the L2S data context

  5. In the Silverlight project, go to add Service Reference, hit "discover" and add the WCF service in

  6. Instantiate the service, and access your methods

Sounds a little complex, but pretty straight forward in practice.

1
votes

Not possible. Silverlight cannot access databases directly. Some sort of web service layer is required in between. I think WCF and RIA Services are the most widely used.

1
votes

I would strongly recommend that you clear an hour in your schedule and just watch this video:-
net-ria-services-intro

In fact clear 2 hours and work along side the video building your own copy of the app being built.

1
votes

If you want to develop a regular LOB (Line Of Business) Application, then you should follow the approach using a web service interface to your database. The RIA services or WCF data service is the current RAD (Rapid Application Development) technology by Microsoft to make this task easier.

If you're talking about a special scenario you need: Silverlight 4 when running out of browser can talk to COM servers on windows now. Talking to databases is described in this vast blog post: Cutting Edge Silverlight 4 Com Features.

If you're looking for a way to manipulate database-like files, you may take a look at the csharp-sqlite project. I think it compiles for Silverlight without much ado (quick and dirty proof of concept here: Proof of Concept csharp-sqlite in Silverlight). With it you could create and manipulate a database file in the user's isolated file storage in a regular Silverlight application and then upload it to wherever you desire.