Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use correct entityType for chained navigations #4334

Merged
merged 1 commit into from
Jan 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ public virtual JoinExpressionBase AddOuterJoin(
return outerJoinExpression;
}

public string CreateUniqueTableAlias()
public virtual string CreateUniqueTableAlias()
=> CreateUniqueTableAlias(SystemAliasPrefix);

private string CreateUniqueTableAlias(string currentAlias)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,27 +394,26 @@ var accessorLambda
private IEnumerable<INavigation> BindChainedNavigations(
IEnumerable<INavigation> boundNavigations, IReadOnlyList<PropertyInfo> chainedNavigationProperties)
{
var boundChainedNavigations = new List<INavigation>();
var boundNavigationsList = boundNavigations.ToList();

if (chainedNavigationProperties != null)
{
foreach (
var navigation in
from propertyInfo in chainedNavigationProperties
// ReSharper disable once AssignNullToNotNullAttribute
let entityType = QueryCompilationContext.Model.FindEntityType(propertyInfo.DeclaringType)
select entityType?.FindNavigation(propertyInfo.Name))
foreach (var propertyInfo in chainedNavigationProperties)
{
var lastNavigation = boundNavigationsList.Last();
var navigation = lastNavigation.GetTargetType().FindNavigation(propertyInfo.Name);

if (navigation == null)
{
return null;
}

boundChainedNavigations.Add(navigation);
boundNavigationsList.Add(navigation);
}

}

return boundNavigations.Concat(boundChainedNavigations);
return boundNavigationsList;
}

protected virtual void IncludeNavigations(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,16 @@ protected virtual void OnModelCreating(ModelBuilder modelBuilder)
.WithOne(e => e.ParentCollection)
.HasForeignKey(e => e.ParentCollectionId)
.IsRequired(false);

modelBuilder.Entity<PrincipalEntity>()
.HasOne(e => e.Reference)
.WithMany()
.IsRequired(false);

modelBuilder.Entity<ReferencedEntity>()
.HasMany(e => e.Principals)
.WithOne()
.IsRequired(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,18 @@ public virtual void Nested_include_with_inheritance_collection_collection_revers
}
}

[Fact]
public virtual void Nested_include_collection_reference_on_non_entity_base()
{
using (var context = CreateContext())
{
var query = context.ReferencedEntities.Include(e => e.Principals).ThenInclude(e => e.Reference);
var result = query.ToList();

Assert.Equal(2, result.Count);
}
}

protected InheritanceRelationshipsContext CreateContext() => Fixture.CreateContext(TestStore);

protected InheritanceRelationshipsQueryTestBase(TFixture fixture)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,8 @@ public InheritanceRelationshipsContext(IServiceProvider serviceProvider, DbConte
public DbSet<CollectionOnBase> CollectionsOnBase { get; set; }
public DbSet<CollectionOnDerived> CollectionsOnDerived { get; set; }
public DbSet<NestedCollectionBase> NestedCollections { get; set; }

public DbSet<PrincipalEntity> PrincipalEntities { get; set; }
public DbSet<ReferencedEntity> ReferencedEntities { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,25 @@ public static void Seed(InheritanceRelationshipsContext context)
baseEntity2.DerivedSefReferenceOnBase = derivedEntity2;
baseEntity3.DerivedSefReferenceOnBase = derivedEntity3;

var principalEntity1 = new PrincipalEntity();
var principalEntity2 = new PrincipalEntity();

context.PrincipalEntities.AddRange(principalEntity1, principalEntity2);

var referencedEntity1 = new ReferencedEntity
{
Principals = new List<PrincipalEntity> { principalEntity1 }
};
var referencedEntity2 = new ReferencedEntity
{
Principals = new List<PrincipalEntity> { principalEntity2 }
};

context.ReferencedEntities.AddRange(referencedEntity1, referencedEntity2);

principalEntity2.Reference = referencedEntity1;
principalEntity1.Reference = referencedEntity2;

context.SaveChanges();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace Microsoft.EntityFrameworkCore.FunctionalTests.TestModels.InheritanceRelationships
{
public class NonEntityBase
{
public int Id { get; set; }
public ReferencedEntity Reference { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace Microsoft.EntityFrameworkCore.FunctionalTests.TestModels.InheritanceRelationships
{
public class PrincipalEntity : NonEntityBase
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections.Generic;

namespace Microsoft.EntityFrameworkCore.FunctionalTests.TestModels.InheritanceRelationships
{
public class ReferencedEntity
{
public int Id { get; set; }

public ICollection<PrincipalEntity> Principals { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,26 @@ WHERE [e].[Discriminator] IN ('NestedCollectionDerived', 'NestedCollectionBase')
Sql);
}

public override void Nested_include_collection_reference_on_non_entity_base()
{
base.Nested_include_collection_reference_on_non_entity_base();

Assert.Equal(
@"SELECT [e].[Id]
FROM [ReferencedEntity] AS [e]
ORDER BY [e].[Id]

SELECT [p].[Id], [p].[ReferenceId], [p].[ReferencedEntityId], [r].[Id]
FROM [PrincipalEntity] AS [p]
INNER JOIN (
SELECT DISTINCT [e].[Id]
FROM [ReferencedEntity] AS [e]
) AS [e] ON [p].[ReferencedEntityId] = [e].[Id]
LEFT JOIN [ReferencedEntity] AS [r] ON [p].[ReferenceId] = [r].[Id]
ORDER BY [e].[Id]",
Sql);
}

protected override void ClearLog()
{
}
Expand Down