Skip to content

Commit

Permalink
#34 fix select only the navigation property
Browse files Browse the repository at this point in the history
  • Loading branch information
voronov-maxim committed Jun 4, 2019
1 parent d422c99 commit 1dcbdeb
Show file tree
Hide file tree
Showing 22 changed files with 207 additions and 123 deletions.
2 changes: 1 addition & 1 deletion build/dependencies.props
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<NetStandardVersion>netstandard2.0</NetStandardVersion>
<NetCoreAppVersion>netcoreapp2.2</NetCoreAppVersion>
<ODataLibVersion>7.5.4</ODataLibVersion>
<OdataToEntityVersion>2.3.0</OdataToEntityVersion>
<OdataToEntityVersion>2.3.1</OdataToEntityVersion>
<SystemInteractiveAsyncVersion>3.2.0</SystemInteractiveAsyncVersion>
<TestSdkVersion>16.1.1</TestSdkVersion>
<Version>$(OdataToEntityVersion)</Version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public override DynamicDependentPropertyInfo GetDependentProperties(String table
SchemaContext schemaContext = _dbContextPool.Rent();
try
{
(String, String) tableFullName = _schemaCache.GetTables(schemaContext)[tableName];
if (_schemaCache.GetNavigations(schemaContext).TryGetValue(tableFullName, out List<SchemaCache.Navigation> navigations))
(String tableSchema, String tableName, bool isQueryType) tableFullName = _schemaCache.GetTables(schemaContext)[tableName];
if (_schemaCache.GetNavigations(schemaContext).TryGetValue((tableFullName.tableSchema, tableFullName.tableName), out List<SchemaCache.Navigation> navigations))
foreach (SchemaCache.Navigation navigation in navigations)
if (navigation.NavigationName == navigationPropertyName)
{
Expand Down Expand Up @@ -77,13 +77,13 @@ public override String GetEntityName(String tableName)
if (!String.IsNullOrEmpty(navigationMapping.ManyToManyTarget))
yield return (navigationMapping.NavigationName, navigationMapping.ManyToManyTarget);
}
public override IEnumerable<String> GetNavigationProperties(String tableName)
public override IEnumerable<String> GetNavigationProperties(String tableEdmName)
{
SchemaContext schemaContext = _dbContextPool.Rent();
try
{
(String, String) tableFullName = _schemaCache.GetTables(schemaContext)[tableName];
if (_schemaCache.GetNavigations(schemaContext).TryGetValue(tableFullName, out List<SchemaCache.Navigation> navigations))
(String tableSchema, String tableName, bool isQueryType) tableFullName = _schemaCache.GetTables(schemaContext)[tableEdmName];
if (_schemaCache.GetNavigations(schemaContext).TryGetValue((tableFullName.tableSchema, tableFullName.tableName), out List<SchemaCache.Navigation> navigations))
foreach (SchemaCache.Navigation navigation in navigations)
yield return navigation.NavigationName;
}
Expand All @@ -92,11 +92,20 @@ public override IEnumerable<String> GetNavigationProperties(String tableName)
_dbContextPool.Return(schemaContext);
}
}
public override IEnumerable<String> GetPrimaryKey(String tableName)
public override IEnumerable<String> GetPrimaryKey(String tableEdmName)
{
foreach (DbColumn column in _schemaCache.GetColumns(tableName))
if (column.IsKey.GetValueOrDefault())
yield return column.ColumnName;
SchemaContext schemaContext = _dbContextPool.Rent();
try
{
(String tableSchema, String tableName) tableFullName = _schemaCache.GetTableFullName(tableEdmName);
String constraintName = _schemaCache.GetPrimaryKeyConstraintNames(schemaContext)[tableFullName];
List<KeyColumnUsage> keyColumns = _schemaCache.GetKeyColumns(schemaContext)[(tableFullName.tableSchema, constraintName)];
return keyColumns.OrderBy(c => c.OrdinalPosition).Select(c => c.ColumnName);
}
finally
{
_dbContextPool.Return(schemaContext);
}
}
public override IEnumerable<DynamicPropertyInfo> GetStructuralProperties(String tableName)
{
Expand All @@ -121,13 +130,13 @@ public override String GetTableName(String entityName)
{
return entityName;
}
public override IEnumerable<String> GetTableNames()
public override IEnumerable<(String tableEdmName, bool isQueryType)> GetTableNames()
{
SchemaContext schemaContext = _dbContextPool.Rent();
try
{
ICollection<String> tableNames = _schemaCache.GetTables(schemaContext).Keys;
return tableNames;
foreach (var pair in _schemaCache.GetTables(schemaContext))
yield return (pair.Key, pair.Value.isQueryType);
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ private static Db.OeEntitySetAdapterCollection CreateEntitySetAdapters(DynamicTy
var entitySetAdapters = new Db.OeEntitySetAdapter[typeDefinitionManager.TypeDefinitions.Count];
int i = 0;
foreach (DynamicTypeDefinition typeDefinition in typeDefinitionManager.TypeDefinitions)
entitySetAdapters[i++] = CreateEntitySetAdapter(typeDefinition.DynamicTypeType, typeDefinition.TableName, false);
entitySetAdapters[i++] = CreateEntitySetAdapter(typeDefinition.DynamicTypeType, typeDefinition.TableName, typeDefinition.IsQueryType);
return new Db.OeEntitySetAdapterCollection(entitySetAdapters);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public override IReadOnlyList<PropertyInfo> GetManyToManyProperties(Type clrType
if (properties == null)
properties = new List<PropertyInfo>();

Type itemType = _typeDefinitionManager.GetDynamicTypeDefinition(targetTableName).DynamicTypeType;
Type itemType = _typeDefinitionManager.GetDynamicTypeDefinition(targetTableName, false).DynamicTypeType;
Type propertyType = typeof(ICollection<>).MakeGenericType(itemType);
properties.Add(new OeShadowPropertyInfo(clrType, propertyType, propertyName));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public abstract class DynamicMetadataProvider
public abstract IEnumerable<String> GetPrimaryKey(String tableEdmName);
public abstract IEnumerable<DynamicPropertyInfo> GetStructuralProperties(String tableEdmName);
public abstract String GetTableName(String entityName);
public abstract IEnumerable<String> GetTableNames();
public abstract IEnumerable<(String tableEdmName, bool isQueryType)> GetTableNames();

public abstract DbContextOptions DbContextOptions { get; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ public DynamicModelBuilder(DynamicTypeDefinitionManager typeDefinitionManager)

public void Build(Microsoft.EntityFrameworkCore.ModelBuilder modelBuilder)
{
foreach (String tableName in MetadataProvider.GetTableNames())
foreach ((String tableEdmName, bool isQueryType) in MetadataProvider.GetTableNames())
{
CreateEntityType(modelBuilder, tableName);
CreateNavigationProperties(modelBuilder, tableName);
CreateEntityType(modelBuilder, tableEdmName, isQueryType);
CreateNavigationProperties(modelBuilder, tableEdmName);
}
}
private EntityType CreateEntityType(Microsoft.EntityFrameworkCore.ModelBuilder modelBuilder, String tableName)
private EntityType CreateEntityType(Microsoft.EntityFrameworkCore.ModelBuilder modelBuilder, String tableName, bool isQueryType)
{
if (!_entityTypes.TryGetValue(tableName, out EntityType entityType))
{
var dynamicTypeDefinition = TypeDefinitionManager.GetDynamicTypeDefinition(tableName);
var dynamicTypeDefinition = TypeDefinitionManager.GetDynamicTypeDefinition(tableName, isQueryType);
EntityTypeBuilder entityTypeBuilder = modelBuilder.Entity(dynamicTypeDefinition.DynamicTypeType).ToTable(tableName);

entityType = (EntityType)entityTypeBuilder.Metadata;
Expand All @@ -47,7 +47,10 @@ private EntityType CreateEntityType(Microsoft.EntityFrameworkCore.ModelBuilder m
propertyBuilder.ValueGeneratedNever();
}

entityTypeBuilder.HasKey(MetadataProvider.GetPrimaryKey(tableName).ToArray());
if (isQueryType)
entityTypeBuilder.Metadata.IsQueryType = true;
else
entityTypeBuilder.HasKey(MetadataProvider.GetPrimaryKey(tableName).ToArray());

_entityTypes.Add(tableName, entityType);
}
Expand All @@ -60,8 +63,8 @@ private void CreateNavigationProperties(Microsoft.EntityFrameworkCore.ModelBuild
{
DynamicDependentPropertyInfo dependentInfo = MetadataProvider.GetDependentProperties(tableName, propertyName);

EntityType dependentEntityType = CreateEntityType(modelBuilder, MetadataProvider.GetTableName(dependentInfo.DependentEntityName));
EntityType principalEntityType = CreateEntityType(modelBuilder, MetadataProvider.GetTableName(dependentInfo.PrincipalEntityName));
EntityType dependentEntityType = CreateEntityType(modelBuilder, MetadataProvider.GetTableName(dependentInfo.DependentEntityName), false);
EntityType principalEntityType = CreateEntityType(modelBuilder, MetadataProvider.GetTableName(dependentInfo.PrincipalEntityName), false);

var dependentProperties = new List<Property>();
foreach (String dependentPropertyName in dependentInfo.DependentPropertyNames)
Expand All @@ -81,7 +84,7 @@ private void CreateNavigationProperties(Microsoft.EntityFrameworkCore.ModelBuild
fkey = dependentEntityType.AddForeignKey(dependentProperties, pkey, principalEntityType);
}

DynamicTypeDefinition dynamicTypeDefinition = TypeDefinitionManager.GetDynamicTypeDefinition(tableName);
DynamicTypeDefinition dynamicTypeDefinition = TypeDefinitionManager.GetDynamicTypeDefinition(tableName, false);
if (dependentInfo.IsCollection)
{
Navigation navigation = fkey.HasPrincipalToDependent(propertyName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ public ShadowPropertyDefinition(MethodInfo methodGet, FieldInfo fieldInfo)
private readonly Dictionary<String, FieldInfo> _shadowPropertyFieldInfoByGetName;
private int _singleFieldIndex;

public DynamicTypeDefinition(Type dynamicTypeType, String entityName, String tableName)
public DynamicTypeDefinition(Type dynamicTypeType, String entityName, String tableName, bool isQueryType)
{
DynamicTypeType = dynamicTypeType;
EntityName = entityName;
TableName = tableName;
IsQueryType = isQueryType;

_navigationPropertyNames = new Dictionary<String, String>();
_shadowPropertyDefinitions = new Dictionary<String, ShadowPropertyDefinition>();
Expand Down Expand Up @@ -98,6 +99,7 @@ public String GetSingleFiledName(String navigationPropertyName)

public Type DynamicTypeType { get; }
public String EntityName { get; }
public bool IsQueryType { get; }
public IReadOnlyCollection<String> PropertyNames => _navigationPropertyNames.Keys;
public String TableName { get; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ public static IQueryable<DynamicType> GetQueryable(DynamicDbContext dynamicDbCon
ConstructorInfo ctor = dbSetType.GetConstructor(new Type[] { typeof(IAsyncQueryProvider) });
return (IQueryable<DynamicType>)ctor.Invoke(new Object[] { dynamicDbContext.GetDependencies().QueryProvider });
}
public DynamicTypeDefinition GetDynamicTypeDefinition(String tableName)
public DynamicTypeDefinition GetDynamicTypeDefinition(String tableName, bool isQueryType)
{
if (_tableNameTypes.TryGetValue(tableName, out Type dynamicTypeType))
return GetDynamicTypeDefinition(dynamicTypeType);

dynamicTypeType = GetDynamicTypeType();
String entityName = MetadataProvider.GetEntityName(tableName);
var dynamicTypeDefinition = new DynamicTypeDefinition(dynamicTypeType, entityName, tableName);
var dynamicTypeDefinition = new DynamicTypeDefinition(dynamicTypeType, entityName, tableName, isQueryType);
_tableNameTypes.Add(tableName, dynamicTypeType);
_dynamicTypeDefinitions.Add(dynamicTypeType, dynamicTypeDefinition);
return dynamicTypeDefinition;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ public override String GetTableName(String entityName)

throw new InvalidOperationException("Table for entity name " + entityName + " not found");
}
public override IEnumerable<String> GetTableNames()
public override IEnumerable<(String tableEdmName, bool isQueryType)> GetTableNames()
{
foreach (IEdmEntitySet entitySet in _edmModel.EntityContainer.EntitySets())
yield return entitySet.Name;
yield return (entitySet.Name, false);
}

public override DbContextOptions DbContextOptions => throw new NotImplementedException();
Expand Down
Loading

0 comments on commit 1dcbdeb

Please sign in to comment.