# The DataServiceContext

**RadDataServiceDataSource** requires a **DataServiceContext** and a **DataServiceQuery\<T>** in order to connect to a remote server.

## The DataServiceContext

A class that derives from the **DataServiceContext** class will be automatically generated for you when you add the **Service Reference** to your project. It can be located in the generated code-behind file of your **Service Reference**. You might need to select the ***Show All Files*** option in **Solution Explorer** in order to see the code-behind file.

![WPF RadDataServiceDataSource Generated Files WPF](https://www.telerik.com/products/wpf/documentation/assets/3a1ec187942bed972525fe65db32e7f4/raddataservicedatasource_generatedclientcode_wpf.png)

A typical auto-generated **DataServiceContext** class will look like this:

```C#

public partial class NorthwindEntities : global::System.Data.Services.Client.DataServiceContext
```

```VB.NET

Partial Public Class NorthwindEntities Inherits Global.System.Data.Services.Client.DataServiceContext
```

## Setting DataServiceContext in code-behind

In order to create an instance of the DataServiceContext class, you need to specify the WCF Data Service Uri in its constructor. The simply assign to the respective property on RadDataServiceDataSource:

```C#

Uri serviceUri = new Uri("http://services.odata.org/Northwind/Northwind.svc", UriKind.Absolute);
NorthwindEntities northwindContext = new NorthwindEntities(serviceUri);
this.radDataServiceDataSource.DataServiceContext = northwindContext;
```

```VB.NET

Dim serviceUri As New Uri("http://services.odata.org/Northwind/Northwind.svc", UriKind.Absolute)
Dim northwindContext As New NorthwindEntities(serviceUri)
Me.radDataServiceDataSource.DataServiceContext = northwindContextSetting DataServiceContext in XAML
```

## Setting DataServiceContext in XAML

Since the **DataServiceContext** class does not have a parameterless constructor, it cannot be directly instantiated in XAML. This can be worked around by creating another class with a parameterless constructor that derives from it and then hard-coding the **Uri** in the call to the base constructor:

```C#

public class MyNorthwindContext : NorthwindEntities
{
    public MyNorthwindContext() : base(new Uri("http://services.odata.org/Northwind/Northwind.svc", UriKind.Absolute)) { }
}
```

```VB.NET

Public Class MyNorthwindContext
 Inherits NorthwindEntities
 Public Sub New()
  MyBase.New(New Uri("http://services.odata.org/Northwind/Northwind.svc", UriKind.Absolute))
 End Sub
End Class
```

Once you have a context class with a parameterless constructor, you can easily instantiate it in XAML:

```XAML

<telerik:RadDataServiceDataSource Name="customersDataSource" QueryName="Customers" AutoLoad="True">
    <telerik:RadDataServiceDataSource.DataServiceContext>
        <local:MyNorthwindContext/>
    </telerik:RadDataServiceDataSource.DataServiceContext>
</telerik:RadDataServiceDataSource>
```

> caution Once you assign the **DataServiceContext** to the control, do not set any of its properties or call any of its methods. **RadDataServiceDataSource** needs to be the only entity that operates with the context. **WCF Data Services**\_ are stateless, but the **DataServiceContext** is not. Tampering with the **DataServiceContext** of a **RadDataServiceDataSource** may lead to unexpected results. In case you need to perform unrelated tasks, you can always create another context and work with it, instead of modifying the one that is currently in use by **RadDataServiceDataSource**.

> The only ***DataServiceContext.MergeOption*** that is supported is ***MergeOption.OverwriteChanges***. Setting the ***DataServiceContext.MergeOption*** to anything else will result in an error.
