Skip to content

Commit

Permalink
fix more then one many-to-many relations
Browse files Browse the repository at this point in the history
  • Loading branch information
voronov-maxim committed Oct 31, 2018
1 parent 24a4f54 commit beba013
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 63 deletions.
8 changes: 4 additions & 4 deletions build/dependencies.props
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
<JsonNetVersion>10.0.3</JsonNetVersion>
<NetStandardVersion>netstandard2.0</NetStandardVersion>
<NetCoreAppVersion>netcoreapp2.1</NetCoreAppVersion>
<ODataLibVersion>7.5.0</ODataLibVersion>
<OdataToEntityVersion>2.0.0</OdataToEntityVersion>
<ODataLibVersion>7.5.1</ODataLibVersion>
<OdataToEntityVersion>2.0.1</OdataToEntityVersion>
<SystemInteractiveAsyncVersion>3.2.0</SystemInteractiveAsyncVersion>
<TestSdkVersion>15.8.0</TestSdkVersion>
<TestSdkVersion>15.9.0</TestSdkVersion>
<Version>$(OdataToEntityVersion)</Version>
<XunitVersion>2.4.0</XunitVersion>
<XunitVersion>2.4.1</XunitVersion>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion source/OdataToEntity.Linq2Db/OdataToEntity.Linq2Db.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="linq2db" Version="2.2.0" />
<PackageReference Include="linq2db" Version="2.4.0" />
<PackageReference Include="System.ComponentModel.Annotations" Version="$(CoreFxVersion)" />
</ItemGroup>

Expand Down
1 change: 1 addition & 0 deletions source/OdataToEntity/Db/OeDbEnumerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ private void Initialize()

if (_uniqueConstraint != null)
{
_uniqueConstraint.Clear();
Object value = Current;
if (value != null)
_uniqueConstraint.Add(value);
Expand Down
67 changes: 30 additions & 37 deletions source/OdataToEntity/ModelBuilder/ManyToManyBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,35 @@ public ManyToManyBuilder(IEdmModel edmModel, OeEdmModelMetadataProvider metadata

public void Build(EntityTypeInfo typeInfo)
{
(PropertyInfo many, PropertyInfo join) = GetManyToManyInfo(_metadataProvider, typeInfo.ClrType);
if (many == null || many.DeclaringType != typeInfo.ClrType)
return;
foreach ((PropertyInfo many, PropertyInfo join) in GetManyToManyInfo(_metadataProvider, typeInfo.ClrType))
{
if (many == null || many.DeclaringType != typeInfo.ClrType)
continue;

IEdmNavigationProperty joinNavigationProperty = GetJoinNavigationProperty(typeInfo, join.DeclaringType);
if (joinNavigationProperty == null)
return;
IEdmNavigationProperty joinNavigationProperty = GetJoinNavigationProperty(typeInfo, join.DeclaringType);
if (joinNavigationProperty == null)
continue;

IEdmNavigationProperty targetNavigationProperty = GetTargetNavigationProperty(_entityTypeInfos[join.DeclaringType], join.PropertyType);
if (targetNavigationProperty == null)
return;
var targetNavigationProperty = (IEdmNavigationProperty)_entityTypeInfos[join.DeclaringType].EdmType.FindProperty(join.Name);
if (targetNavigationProperty == null)
continue;

EntityTypeInfo principalInfo = _entityTypeInfos[join.PropertyType];
EntityTypeInfo dependentInfo = _entityTypeInfos[many.DeclaringType];
var edmDependentInfo = new EdmNavigationPropertyInfo()
{
ContainsTarget = true,
Name = many.Name,
OnDelete = EdmOnDeleteAction.None,
PrincipalProperties = principalInfo.EdmType.DeclaredKey,
Target = principalInfo.EdmType,
TargetMultiplicity = EdmMultiplicity.Many
};
EdmNavigationProperty edmManyToManyProperty = dependentInfo.EdmType.AddUnidirectionalNavigation(edmDependentInfo);
EntityTypeInfo principalInfo = _entityTypeInfos[join.PropertyType];
EntityTypeInfo dependentInfo = _entityTypeInfos[many.DeclaringType];
var edmDependentInfo = new EdmNavigationPropertyInfo()
{
ContainsTarget = true,
Name = many.Name,
OnDelete = EdmOnDeleteAction.None,
PrincipalProperties = principalInfo.EdmType.DeclaredKey,
Target = principalInfo.EdmType,
TargetMultiplicity = EdmMultiplicity.Many
};
EdmNavigationProperty edmManyToManyProperty = dependentInfo.EdmType.AddUnidirectionalNavigation(edmDependentInfo);

var manyToManyJoinDescription = new ManyToManyJoinDescription(join.DeclaringType, joinNavigationProperty, targetNavigationProperty);
_edmModel.SetAnnotationValue(edmManyToManyProperty, manyToManyJoinDescription);
var manyToManyJoinDescription = new ManyToManyJoinDescription(join.DeclaringType, joinNavigationProperty, targetNavigationProperty);
_edmModel.SetAnnotationValue(edmManyToManyProperty, manyToManyJoinDescription);
}
}
private static IEdmNavigationProperty GetJoinNavigationProperty(EntityTypeInfo typeInfo, Type joinClassType)
{
Expand All @@ -61,7 +63,7 @@ private static IEdmNavigationProperty GetJoinNavigationProperty(EntityTypeInfo t

return null;
}
private static (PropertyInfo Many, PropertyInfo Join) GetManyToManyInfo(OeEdmModelMetadataProvider metadataProvider, Type entityType)
private static IEnumerable<(PropertyInfo Many, PropertyInfo Join)> GetManyToManyInfo(OeEdmModelMetadataProvider metadataProvider, Type entityType)
{
var collectionProperties = new List<PropertyInfo>();
foreach (PropertyInfo propertyInfo in entityType.GetProperties())
Expand All @@ -79,11 +81,12 @@ private static (PropertyInfo Many, PropertyInfo Join) GetManyToManyInfo(OeEdmMod
Type itemType2 = Parsers.OeExpressionHelper.GetCollectionItemType(propertyInfo2.PropertyType);
PropertyInfo partnerProperty = GetPartnerProperty(metadataProvider, itemType, itemType2);
if (partnerProperty != null && itemType == partnerProperty.PropertyType)
return (propertyInfo, partnerProperty);
{
yield return (propertyInfo, partnerProperty);
break;
}
}
}

return default;
}
private static PropertyInfo GetPartnerProperty(OeEdmModelMetadataProvider metadataProvider, Type itemType, Type itemType2)
{
Expand Down Expand Up @@ -130,15 +133,5 @@ private static PropertyInfo GetPartnerProperty(OeEdmModelMetadataProvider metada

return partnerProperty;
}
private static IEdmNavigationProperty GetTargetNavigationProperty(EntityTypeInfo typeInfo, Type targetType)
{
foreach (PropertyInfo propertyInfo in typeInfo.ClrType.GetProperties())
if (propertyInfo.PropertyType == targetType)
foreach (IEdmNavigationProperty edmNavigationProperty in typeInfo.EdmType.NavigationProperties())
if (String.CompareOrdinal(edmNavigationProperty.Name, propertyInfo.Name) == 0)
return edmNavigationProperty;

return null;
}
}
}
18 changes: 4 additions & 14 deletions source/OdataToEntity/Parsers/Translators/OeJoinBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,8 @@ private static LambdaExpression GetGroupJoinInnerKeySelector(Type innerType, IEd
structuralProperties = edmNavigationProperty.Partner.DependentProperties();
else
{
if (edmNavigationProperty.Partner == null)
{
var structuralPropertyList = new List<IEdmStructuralProperty>();
foreach (EdmReferentialConstraintPropertyPair constraintPropertyPair in edmNavigationProperty.ReferentialConstraint.PropertyPairs)
structuralPropertyList.Add(constraintPropertyPair.DependentProperty);
structuralProperties = structuralPropertyList;
}
if (edmNavigationProperty.Type.IsCollection())
structuralProperties = edmNavigationProperty.DependentProperties();
else
structuralProperties = edmNavigationProperty.PrincipalProperties();
}
Expand Down Expand Up @@ -193,13 +188,8 @@ private LambdaExpression GetGroupJoinOuterKeySelector(Type outerType, IEdmNaviga
structuralProperties = edmNavigationProperty.Partner.PrincipalProperties();
else
{
if (edmNavigationProperty.Partner == null)
{
var structuralPropertyList = new List<IEdmStructuralProperty>();
foreach (EdmReferentialConstraintPropertyPair constraintPropertyPair in edmNavigationProperty.ReferentialConstraint.PropertyPairs)
structuralPropertyList.Add(constraintPropertyPair.PrincipalProperty);
structuralProperties = structuralPropertyList;
}
if (edmNavigationProperty.Type.IsCollection())
structuralProperties = edmNavigationProperty.PrincipalProperties();
else
structuralProperties = edmNavigationProperty.DependentProperties();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="linq2db" Version="2.2.0" />
<PackageReference Include="linq2db.SqlServer" Version="2.2.0" />
<PackageReference Include="linq2db" Version="2.4.0" />
<PackageReference Include="linq2db.SqlServer" Version="2.4.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="$(EfCoreVersion)" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="$(EfCoreVersion)" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(TestSdkVersion)" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="linq2db" Version="2.2.0" />
<PackageReference Include="linq2db.SqlServer" Version="2.2.0" />
<PackageReference Include="linq2db.t4models" Version="1.10.0" />
<PackageReference Include="linq2db" Version="2.4.0" />
<PackageReference Include="linq2db.SqlServer" Version="2.4.0" />
<PackageReference Include="linq2db.t4models" Version="2.4.0" />
<PackageReference Include="System.ComponentModel.Annotations" Version="$(CoreFxVersion)" />
</ItemGroup>

Expand Down
4 changes: 2 additions & 2 deletions test/OdataToEntity.Test/Common/TestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,11 @@ private static MemberExpression[] GetKeyExpressions(ModelBuilder.OeEdmModelMetad
}
private static String RemoveEmptyArrays(String json)
{
String[] lines = json.Split(Environment.NewLine);
String[] lines = json.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 1; i < lines.Length; i++)
if (lines[i].EndsWith(": []"))
{
if (lines[i - 1].EndsWith(','))
if (lines[i - 1].EndsWith(","))
lines[i - 1] = lines[i - 1].Remove(lines[i - 1].Length - 1, 1);
lines[i] = null;
}
Expand Down

0 comments on commit beba013

Please sign in to comment.