Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Well, if I understand your question correctly, you can inspect the meta-model for a data context instance. The meta-model describes the tables, columns and associations in your model. Basically you want to look at associations on a table where the association is 1-to-many. </p> <p>This doesn't involve retrieving any data, as you are not actually working with entity instances, just the information that describes them.</p> <p>This code should do it:</p> <pre><code>public static string[] GetChildEntities&lt;T>(DataContext context, T entity) { var mapping = context.Mapping.GetTable(typeof(T)); return mapping.RowType.Associations.Where(a => a.IsMany) .Select(a => a.ThisMember.Name).ToArray(); } </code></pre> <p>This will return the names of any properties that expose the <code>EntitySet</code> instances for the given parent entity.</p> <p><strong>EDIT</strong></p> <p>This code finds the first 1->* association between the parent and child entities based on the meta-model, retrieves the value of the EntitySet property on the parent entity, and adds the child entity to that set. This should work for most basic LINQ to SQL implementations.</p> <pre><code>public static void AddChild&lt;P, C>(DataContext context, P parent, C child) where P : class where C : class { var parentMapping = context.Mapping.GetTable(typeof(P)); var childAssociation = parentMapping.RowType.Associations .Where(a => a.IsMany && a.OtherType.Type == typeof(C)) .FirstOrDefault(); if (childAssociation != null) { var entitySet = (EntitySet&lt;C>) childAssociation.ThisMember .MemberAccessor .GetBoxedValue(parent); entitySet.Add(child); } }</code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload