Many to many relationships with LightSpeed through associations

LightSpeed models many-to-many relationships between entities using a feature called through associations. In a through association, each pair of entities is represented by a third entity of a through type, which maps in the database to a through table.

For example, in a media sharing site, a Contribution may have many Tags, and each Tag may be associated with many Contributions. So you’d represent this using a ContributionTag through table, each row of which represents the relationship between one Contribution and one Tag, but at the LightSpeed level you’d like to work directly with the Contribution.Tags and Tag.Contributions collections rather than down at the ContributionTag level.

Using a Through Association

The nice thing about through associations is that you can work with them as easily as with any other collection. For example, you can use the Add and Remove methods to add Tags to or remove Tags from a Contribution:

public void ThatsNoDromedary(int id)
  Contribution contribution = unitOfWork.FindById<Contribution>(id);
  Tag penguinTag = unitOfWork.Tags.Single(t => t.Value == "penguins");
  Tag zooTag = unitOfWork.Tags.Single(t => t.Value == "zoo");
  Tag dromedaryTag = unitOfWork.Tags.Single(t => t.Value == "dromedaries");
  contribution.Tags.Remove(dromedaryTag);  // it was a mistake anyone could have made

You don’t need to worry about the lower level of the through entity or the through table: LightSpeed takes care of all that bookkeeping for you.

ThroughAssociation also supports other common collection methods such as Clear() and Contains():

public void UntagThatDromedary(int id)
  Contribution contribution = unitOfWork.FindById<Contribution>(id);
  Tag dromedaryTag = unitOfWork.Tags.Single(t => t.Value == "dromedaries");
  if (contribution.Tags.Contains(dromedaryTag))

And just as with EntityCollections it automatically adds newly created items to the unit of work for you:

public void YouMightGetSquished(int id)
  Contribution contribution = unitOfWork.FindById<Contribution>(id);
  Tag heffalumpTag = new Tag { Value = "heffalumps" };
  unitOfWork.SaveChanges();  // heffalumpTag gets inserted into the database

In short, most of the time you can work with a many-to-many ThroughAssociation just as you’d work with a one-to-many EntityCollection.

Defining a Through Association

The easiest way to set up a through association is using the designer. If your through type is ultra-simple — containing nothing but the foreign keys to the entities being related — then you can just draw a through association arrow and tell the designer to create an auto through entity. If you need more control or want to store data against each entity pairing, or if you want to write the classes or associations by hand, you need to know a bit more about how through associations sit over the more fundamental one-to-many and many-to-one associations.

We’ll describe how to do this in the next installment, or for a sneak preview see the documentation.

Tagged as LightSpeed

2 Responses to “Many to many relationships with LightSpeed through associations”

  • Guide to working with Many-to-Many associations with LightSpeed…

    Thank you for submitting this cool story – Trackback from DotNetShoutout…

  • [...] many-to-many associations and I thought it was worth posting here.Recall that in LightSpeed, many-to-many associations are represented using through associations, in which the two associated entities are related via a “through” entity. (The through [...]

  • Leave a Reply


Join our mailer

You should join our newsletter! Sent monthly:

Back to Top