How to have silverlight get its data from MySQL

前端 未结 6 1317

I\'ve written a small hello world test app in Silverlight which i want to host on a Linux/Apache2 server. I want the data to come from MySQL (or some other linux compatible

6条回答
  •  有刺的猬
    2020-12-17 08:03

    I just got this working; ASP.NET4 site with Silverlight4 content on Linux Ubuntu 10 / Apache2 server. Content is developed using Visual Studio 2010. VS2008 should work fine too.

    Server:

    • Setup a Linux server with Apache2 and MySQL, there are tons of guides on this.
      • Make sure MySQL is accessible from the development PC and optionally from the Internet. See here for details: Causes of Access-Denied Errors.
      • Setup the database table structures and add some content for testing later. In our example we assume you have the table 'persons' with the column 'name'.
    • Since Silverlight is a client-side technology you are pretty much good-to-go and can host the application with a simple HTML page.
    • A web service is required between Silverlight and MySQL. Microsoft's WCF RIA is one flavor, but requires .NET. On the plus-side, you get to host ASP.NET4 pages as well. Here is a thorough guide to setting it up: Setting up Mono 2.8 with Asp.Net 4.0 and MVC2 on Ubuntu with MySql Membership

    Visual Studio:

    • Install latest MySQL Connector/Net and restart VS
    • Add your MySQL database as data source
      • Open Server Explorer -> Add data connection -> Select 'MySQL Database'
      • Fill in credentials and test connection

    Setting up the site with MySQL access:

    Here is a guide I found helpful: Step By Step Guide to WCF RIA enabled SL4 application with Entity Framework

    • Create or open a Silverlight project.
      • The server-side project is typically named 'ProjectName.Web'
      • The client-side project is typically named 'ProjectName'
    • Add 'ADO.NET Entity Data Model' to the server project. This will be a model of your database structure.
      • Select 'Generate from database'
      • Choose the MySQL database connection you created
      • Select the tables you want to access
    • Build your solution now before proceeding.
    • Add 'Domain Service Class' to the server project, f.ex. 'FooDomain'. This will make the database entities available to the client-side Silverlight code.
      • In 'Available DataContext/ObjectContext classes:' select the Entity Framework model you created in the previous step.
      • Check the entities you want to access and check 'Enable editing' where appropriate
      • Check 'Generate associated classes for metadata'
    • Build your solution again to generate 'FooDomainContext', based on 'FooDomain' in server project.

    Testing:

    Let's get data from MySQL into Silverlight. Assuming there is a table named 'persons' with column name 'name', we can bind a list box to show the names of the persons.

    First add a Silverlight page, let's say 'Home'. In Home.xaml add:

    
    

    In Home.xaml.cs file add:

    public partial class Home : Page
    {
        public Home()
        {
            InitializeComponent();
    
            Loaded += Home_Loaded;
        }
    
        void Home_Loaded(object sender, RoutedEventArgs e)
        {
            var context = new FooDomainContext();
            var query = context.Load(context.GetPersonsQuery());
            TestList.ItemsSource = query.Entities;
            TestList.DisplayMemberPath = "name";
        }
    }
    

    Here we assume you named your domain service 'FooDomain', and this would generate the 'FooDomainContext' class used.

    Hopefully, if all is set up properly, you will now see a list of person names when running your Silverlight project.

    Edit: ASP.NET is not optional, but required for the WCF RIA web service used in my example.

提交回复
热议问题