Silverlight 5 WCF RIA Services: Part 3


Now we are going to create a app where we would insert data to the database.

I will take up the same project again and add a new page to it.

Silverlight 5 WCF RIA Services

Silverlight 5 WCF RIA Services

I create a Data Model now as shown below:

Silverlight 5 WCF RIA Services

Give it a Build now . Create a new Domain Service class as shown below:

Silverlight 5 WCF RIA Services

Silverlight 5 WCF RIA Services

namespace SL2wayWCFRia.Web
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Data;
    using System.Linq;
    using System.ServiceModel.DomainServices.EntityFramework;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;

    // Implements application logic using the PublishingCompanyEntities context.
    // TODO: Add your application logic to these methods or in additional methods.
    // TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access
    // Also consider adding roles to restrict access as appropriate.
    // [RequiresAuthentication]
    [EnableClientAccess()]
    public class DataDomainService : LinqToEntitiesDomainService<PublishingCompanyEntities>
    {

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Articles' query.
        public IQueryable<Article> GetArticles()
        {
            return this.ObjectContext.Articles;
        }

        public void InsertArticle(Article article)
        {
            if ((article.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(article, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Articles.AddObject(article);
            }
        }

        public void UpdateArticle(Article currentArticle)
        {
            this.ObjectContext.Articles.AttachAsModified(currentArticle, this.ChangeSet.GetOriginal(currentArticle));
        }

        public void DeleteArticle(Article article)
        {
            if ((article.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(article, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.Articles.Attach(article);
                this.ObjectContext.Articles.DeleteObject(article);
            }
        }
    }
}


Silverlight 5 WCF RIA Services

Silverlight 5 WCF RIA Services

Make sure that AutoGenerateColumns="True".

The Designer window will now look as follows:

Silverlight 5 WCF RIA Services


Silverlight 5 WCF RIA Services

Silverlight 5 WCF RIA Services

Now lets add the code for adding an article. Here I would need to manually add the UI for inserting articles.

Silverlight 5 WCF RIA Services

The code behind for the two buttons is provided below:

DataDomainContext context = new DataDomainContext();

private void add_Click(object sender, RoutedEventArgs e)
{
    try
    {
        context.Load(context.GetArticlesQuery());  // Loading the context from the database using the GetArticlesQuery .
        // Creating a new article .
        Article art = new Article();
        art.ArticleID = 101;
        art.AuthorID = 5;
        art.Body = "This is a sample Article";
        art.Title = "This is sample Title";
        context.Articles.Add(art); // Adding the article to the Context
        MessageBox.Show("New article was sucessfully added");
    }
    catch (Exception ex)
    {
        MessageBox.Show("Adding article failed :" + ex.Message);
    }
}

private void save_Click(object sender, RoutedEventArgs e)
{
    try
    {
        context.SubmitChanges();
        MessageBox.Show("The Changes were saved successfully to the DataBase");
    }
    catch (Exception ex)
    {
        MessageBox.Show("The changes were not saved. Try again :" + ex.Message);
    }
}


Run the application again. This time hit the button Add and save. The following messages should appear.

Silverlight 5 WCF RIA Services

Checked the database .

Silverlight 5 WCF RIA Services

Hit refresh to the application.

Silverlight 5 WCF RIA Services

This was a very simple app where we took another step towards creating a end to end Business App. In the next post we would design we would see how to validate our input. Till then. Happy Coding. 

Up Next
    Ebook Download
    View all
    Learn
    View all