Data Access - Fluent Mappings Associations

Associations define the relationships between classes in your fluent mappings. For more information on fluent mappings, see Fluent mappings integration in Sitefinity. According to the relationships between classes there are several types of associations:

  • One-to-Many associations
  • One-to-One associations
  • Many-to-Many associations
  • One Sided Associations

One-to-Many association

This association represent the relationship where for one class there could be many other objects of other classes related. In order to define one-to-many association, you must specify both of the navigation ends. You should use the WithOpposite method and after that create a constraint via the HasConstraint method. When you define mapping for a collection property, the HasAssociation method expects the type of the property to be IList.

Note that in some cases the HasAssociation property might expect the type of the property to be Ilist where T is the type of the data persistent class. This is called One-To-Many Self-Reference Association.

For example in Sitefinity's Librraies the Children property that represents an IList is related to the Parent property of the same class:

 var folderMapping = new MappingConfiguration<Folder>();
 folderMapping.HasAssociation(p => p.Children).WithOpposite(p =>     p.Parent).ToColumn("parent_id").IsManaged();

This means that one folder (Parent) could have several folders (Children).

You could create new parent folder using the following code:

child.Parent = newFolder;

In the sample above the IsManaged method is used making the association managed. This means that you could now create folders using the following code:

parent.Children.Add(newFolder);

The ToColumn method specifies the foreign key column. In the code above the foreign key is going to be parent_id.

One-to-One associations

One-to-One association is similar to the One-to-Many association. However when the members(columns) selected for the source and target entity are Primary Keys, the created association is automatically created as one-to-one instead of one-to-many. The only thing that should be paid attention to, is the IsRequired method call. IsRequired should be defined on the source end of the association so Telerik Data Access "knows" which side should be the source and which the target of the association.

Many-to-Many associations

When you want to create a many-to-many association, instead of the HasConstraint method you should use the MapJoinTable method. It specifies the join table that defines a many to many relationship and its endpoints. In Sitefinity this approach is used widely to define permissions association to the base class. For example in the NewsFluentMapping:

var newsItemMapping = new MappingConfiguration<Telerik.Sitefinity.News.Model.NewsItem>();       newsItemMapping.HasAssociation<Telerik.Sitefinity.Security.Model.Permission>(p => p.Permissions).MapJoinTable();

In the sample above the MapJoinTable method accept no parameters. In this case Telerik Data Access will provide default names to the join table and the relation endpoints (foreign keys).

Similar to the One-to-Many association, you could create a Many-to-Many Self-Reference Associations. In the sample below the association between Page Templates and Permissions is shown where the HasAssociation method expects the type of the property Permissions to be Ilist.

          var pageTemplateMapping = new MappingConfiguration<Telerik.Sitefinity.Pages.Model.PageTemplate>();
      pageTemplateMapping.HasAssociation<Telerik.Sitefinity.Security.Model.Permission>(p => p.Permissions).MapJoinTable("sf_pg_templates_sf_permissions", (p1, p2) => new { id = p1.Id, id2 = p2.Id });

The first parameter of the MapJoinTable method in the sample above is specifying the join table name. The name of the join table is sf_pg_templates_sf_permissions. The second parameters defines the endpoints for the relation (foreign keys).

One Sided Associations

One sided association means that only one navigation end will exist, but not both of them. A sample of One Sided association in Sitefinity is in the PageTemplates:

      pageTemplateMapping.HasAssociation<Telerik.Sitefinity.Pages.Model.PageTemplate>(p => p.ParentTemplate).ToColumn("prent_template_id");

This means that the PageTemplate is having reference to the ParentTemplate, however the ParentTemplate will not have an opposite relation. The ToColumn method specifies the column name that the navigation property is mapped to.

NOTE: when you create one sided associations, the WithOpposite methods are not used.

Make sure to follow the final blog post of the Data Access series, describing the Fetch plans and strategies.

Veronica Milcheva

About Veronica Milcheva

I am a passionate Sitefinity blogger, developer and consultant. In my spare time I enjoy running and listening to music. My personal quote: There's no tough problem, just not enough coffee :)

View Comments

comments powered by Disqus