Skip to content

Instantly share code, notes, and snippets.

@larsw
Created February 5, 2013 13:54
Show Gist options
  • Select an option

  • Save larsw/4714589 to your computer and use it in GitHub Desktop.

Select an option

Save larsw/4714589 to your computer and use it in GitHub Desktop.
EF DbContext-derived (Code-First) context exposing only collection property that .Include() underlying navigational property.
public class MyContext : DbContext
{
protected virtual IDbSet<Field> InnerFields
{
get { return base.Set<Field>(); }
}
protected virtual IDbSet<DimensionDefinition> InnerDimensionDefinitions
{
get { return base.Set<DimensionDefinition>(); }
}
public IQueryable<Field> Fields
{
get { return InnerFields.Include(x => x.Dimensions.Select(y => y.DimensionValue)); }
}
public MyContext()
:base("MyContext")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<DimensionValue>().ToTable(typeof(DimensionValue).Name);
modelBuilder.Entity<Field>().ToTable(typeof(Field).Name);
modelBuilder.Entity<DimensionDefinition>().ToTable(typeof(DimensionDefinition).Name);
modelBuilder.Entity<FieldDefinition>().ToTable(typeof(FieldDefinition).Name);
}
public void AddDimensionDefinition(DimensionDefinition dimensionDefinition)
{
if (dimensionDefinition == null) throw new ArgumentNullException("dimensionDefinition");
InnerDimensionDefinitions.Add(dimensionDefinition);
}
public void AddField(Field field)
{
if (field == null) throw new ArgumentNullException("field");
InnerFields.Add(field);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment