Created
February 5, 2013 13:54
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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