In this recipe, let's use the SQL Server database as the backend. We will create the MyTask
table in the SQL Server Express database.
For this recipe, let's create a WCF service project similar to the last recipe. Let's call this project Recipe3_WCFSQLService
.
In this recipe, we will create the WCF Service project with SQL Server as the backend database. We use the ADO.NET Entity Framework to create all the entity objects, and then we add the methods to return the results to the client application.
MyTasks.mdf
. MyTask
and add the columns as shown in the following screenshot: MyTaskModel
under Model Namespace. Click on the Finish button, as shown in the following screenshot: MyTask.edmx
file is added to the project. When you open this file you should see the model as shown in the following screenshot: MyTaskService.svc
, as shown in the following screenshot. When created, it adds IMyTaskService
and MyTaskService.svc
files. IMyTaskService.cs
file and add a ServiceContract:
[ServiceContract] public interface IMyTaskService { [OperationContract] MyTask GetTask(int value); }
MyTaskService.svc
file and implement the GetTask
function:public MyTask GetTask(int value) { MyTasksEntities _ctx = new MyTasksEntities(); return _ctx.MyTasks.Where(c=>c.Id == value).Single(); }
Recipe3_WcfSqlClient
. MainPage.xaml
file and change the ApplicationTitle
and PageTitle
properties in the TextBlock
controls:<!--TitlePanel contains the name of the application and page title--> <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> <TextBlock x:Name="ApplicationTitle" Text="MY RECIPES" Style="{StaticResource PhoneTextNormalStyle}"/> <TextBlock x:Name="PageTitle" Text="WCF SQL EF" Margin="9,- 7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> </StackPanel>
id
, and a button to click in the first content grid:<!--ContentPanel - place additional content here--> <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0"> <TextBox Height="72" HorizontalAlignment="Left" Margin="0,52,0,0" Name="textBox1" Text="" VerticalAlignment="Top" Width="460" /> <TextBlock Height="30" HorizontalAlignment="Left" Margin="14,32,0,0" Name="textBlock1" Text="Task Id:" VerticalAlignment="Top" /> <Button Content="Get Task Details" Height="72" HorizontalAlignment="Left" Margin="0,116,0,0" Name="button1" VerticalAlignment="Top" Width="243" Click="button1_Click" /> </Grid>
ContentPanel2
. Add four RowDefintions
and two ColumnDefinitions
. Add four TextBlock
controls to display the task details:<!--ContentPanel - place additional content here--> <Grid x:Name="ContentPanel2" Margin="12,217,12,-13" Grid.Row="1"> <Grid.RowDefinitions> <RowDefinition Height="50"/> <RowDefinition Height="50"/> <RowDefinition Height="50"/> <RowDefinition Height="50"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="100" /> <ColumnDefinition Width="300" /> </Grid.ColumnDefinitions> <TextBlock Grid.Row="0" Grid.Column="0" Text="Name:" /> <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Name}" FontWeight="Bold" Foreground="OrangeRed"/> <TextBlock Grid.Row="1" Grid.Column="0" Text="DateDue:" /> <TextBlock Grid.Row="1" Grid.Column="1" Text=" {Binding DateDue}" /> <TextBlock Grid.Row="2" Grid.Column="0" Text="Priority:" /> <TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding Priority}" Foreground="Yellow"/> <TextBlock Grid.Row="3" Grid.Column="0" Text="Notes:" /> <TextBlock Grid.Row="3" Grid.Column="1" Text=" {Binding Notes}" /> </Grid>
MainPage.xaml.cs
file, and add a reference to the WCF service we referenced at the top of the page:using Recipe3_WcfSqlService.ServiceReference1;
MyTaskServiceClient
before the MainPage
constructor:public partial class MainPage : PhoneApplicationPage { MyTaskServiceClient _context; // Constructor public MainPage() { ..
public MainPage() { InitializeComponent(); _context = new MyTaskServiceClient(); _context.GetTaskCompleted += new EventHandler<GetTaskCompletedEventArgs>(GetTaskCompleted); } void GetTaskCompleted(object sender, GetTaskCompletedEventArgs e) { if (e.Result != null) { this.DataContext = e.Result; } }
GetTaskAsync
method with id
as the parameter:private void button1_Click(object sender, RoutedEventArgs e) { if(!String.IsNullOrEmpty(textBox1.Text)) _context.GetTaskAsync(int.Parse(textBox1.Text)); }
All the main functionalities in this recipe are added by the Entity Framework model. Open the MyTask.designer.cs
file and you can see the code generated in two categories: Context and other Entities.
Context class takes care of connecting to the backend database and Entities creates the MyTask
class out of the MyTask
table with all the columns as properties.
In this recipe, we demonstrated how to get the results and display them. The Entity Framework has all the functionalities to update and delete as well. By updating the entity context we can easily update the backend database.