Creating Associations Between Objects in ADO.NET Data Services

December 19, 2008

Creating Associations Between Objects in ADO.NET Data Services

In today’s post I’m Creating Associations Between Objects in ADO.NET Data Services
going to show how to
create an association
between two objects
from a ADO.NET Data
.NET client and
how simple it is.

Creating Associations

Data services expose resources (entities) and links (associations). When
we generate a .NET client proxy the links (associations) are built as properties
of the objects. When we associate an object to another object we have to
explicitly tell the proxy to add the association. This is being done by the
SetLink method which is exposed through the generated client proxy.
The SetLink method notifies the DataServiceContext that a new link exists
between the objects it gets as parameters and to which property the link will
be generated. If you will try to insert an object to an association property and 
won’t use the SetLink method you should expect an exception.

Create Association Example

var proxy = new SchoolEntities(
    new Uri(ConfigurationManager.AppSettings["SchoolProxy"]));
proxy.MergeOption = MergeOption.AppendOnly;
var department = proxy.Department.First();
var course = new Course
    CourseID = 4000,
    Days = "MW",
    Credits = 4,
    Location = "Class A",
    Time = DateTime.Now,
    Title = "Bio Technology",
    Department = department
proxy.AddObject("Course", course);
proxy.SetLink(course, "Department", department);

In the example, there is an association between course and department.

departments can hold many courses and a course is associated to a specific

department. As you can see I set the Department property of course to be

the first department and then I have to explicitly call the SetLink method and

to indicate the property name (Department) in order to set the link.

Currently Implementation Problems

In the Using Microsoft ADO.NET Data Services article it is indicated that the

currently data services implementation “is designed for minimum footprint and

provides the primitives required to enable mapping data service operations to

.NET objects. For a future release, we are evaluating layering higher level semantics”.

This means that managing modifications to arbitrary object graphs with bidirectional

associations isn’t fully supported right now but will be supported in future releases.


Lets sum up, I showed in the post that to set an association between two associated

objects is done by using the SetLink method. You need to explicitly call that method

in order to establish the connection between the objects. Not calling that method will

result in an exception. Currently the data services framework is supporting the

minimum functionality to enable mapping but in future releases we should expect

the ability to manage modifications to arbitrary object graphs with bidirectional


DotNetKicks Image
Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>