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.


I create a Data Model now as shown below:

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


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);
}
}
}
}


Make sure that AutoGenerateColumns="True".
The Designer window will now look as follows:



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

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.

Checked the database .

Hit refresh to the application.

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.

yonathan BenitahPosted Nov 29, 2012, 6:23 AM EST
Hi, I have the following architecture: DB - DAL - BL - webService - Aspx I would like to add a new client (Silverlight app), but don't want it to depend on the aspx (only being hosted by the aspx). Also, i would like to use the RIAservice. Have you got an issue for me? Thx....
RicardoPosted Aug 24, 2011, 9:59 PM EDT
Dear Mahadesh, First of all, thank you for this article (series of articles) about wcf ria services with silverlight. I've been struggling with it for quite a while untill now. But in your code when you create a new article you say art.ID = 101; And when you retrieve it with SQL from the database you say WHERE ArticleID = 20 This has to be a typo, or have you used a database table with identity specification on the ArticleID column? If that's so, the code has to break because you can't assign the ArticleID, or am I wrong? Please give me some clearity about this. Kind regards, Ricardo