private void ProcessTable(LinqMetaTable table, LinqMetaType rowType, string name, PropertyInfo prop) { DLinqTables.Add(new DLinqTableProvider(this, rowType, name, prop)); foreach (LinqMetaType derivedType in rowType.DerivedTypes) { ProcessTable(table, derivedType, derivedType.Name, prop); } }
/// <summary> /// Set the DataLoadOptions on a Linq To Sql datasource to force all the FK entities /// to be directly loaded. /// </summary> /// <param name="dataSource">The data source for which we want to preload FKs</param> /// <param name="rowType">The type of the entities returned by the data source</param> public static void LoadWithForeignKeys(this LinqDataSource dataSource, Type rowType) { dataSource.ContextCreated += delegate(object sender, LinqDataSourceStatusEventArgs e) { // This only applies to a DLinq data context var context = e.Result as DataContext; if (context == null) { return; } DataLoadOptions loadOptions = null; ParameterExpression tableParameter = null; System.Data.Linq.Mapping.MetaTable metaTable = context.Mapping.GetTable(rowType); foreach (System.Data.Linq.Mapping.MetaDataMember member in metaTable.RowType.DataMembers) { if (member.IsAssociation && !member.Association.IsMany) { if (member.Type.Equals(rowType)) { continue; } if (loadOptions == null) { loadOptions = new DataLoadOptions(); tableParameter = Expression.Parameter(rowType, "e"); } var memberExpression = Expression.Property(tableParameter, member.Name); loadOptions.LoadWith(Expression.Lambda(memberExpression, tableParameter)); } } if (loadOptions != null) { context.LoadOptions = loadOptions; } }; }
public DLinqDataModelProvider(object contextInstance, Func <object> contextFactory) { ContextFactory = contextFactory; DataContext context = (DataContext)contextInstance ?? (DataContext)CreateContext(); ContextType = context.GetType(); DLinqTables = new List <TableProvider>(); foreach (PropertyInfo prop in ContextType.GetProperties()) { Type entityType = GetEntityType(prop); if (entityType != null) { LinqMetaTable table = GetLinqTable(context, entityType); ProcessTable(table, table.RowType, prop.Name, prop); } } DLinqTables.ForEach(t => ((DLinqTableProvider)t).Initialize()); _roTables = new ReadOnlyCollection <TableProvider>(DLinqTables); }
private void ProcessTable(LinqMetaTable table, LinqMetaType rowType, string name, PropertyInfo prop) { DLinqTables.Add(new DLinqTableProvider(this, rowType, name, prop)); foreach (LinqMetaType derivedType in rowType.DerivedTypes) ProcessTable(table, derivedType, derivedType.Name, prop); }