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

Query: Add regression test for #18582 #25685

Merged
merged 1 commit into from
Aug 25, 2021
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
78 changes: 78 additions & 0 deletions test/EFCore.Specification.Tests/Query/OwnedEntityQueryTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,5 +230,83 @@ protected class PostDto24133
public string Title { get; set; }
public int CommentsCount { get; set; }
}

[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Projecting_correlated_collection_property_for_owned_entity(bool async)
{
var contextFactory = await InitializeAsync<MyContext18582>(seed: c => c.Seed());

using var context = contextFactory.CreateContext();
var query = context.Warehouses.Select(x => new WarehouseModel
{
WarehouseCode = x.WarehouseCode,
DestinationCountryCodes = x.DestinationCountries.Select(c => c.CountryCode).ToArray()
}).AsNoTracking();

var result = async
? await query.ToListAsync()
: query.ToList();

var warehouseModel = Assert.Single(result);
Assert.Equal("W001", warehouseModel.WarehouseCode);
Assert.True(new[] { "US", "CA" }.SequenceEqual(warehouseModel.DestinationCountryCodes));
}

protected class MyContext18582 : DbContext
{
public MyContext18582(DbContextOptions options)
: base(options)
{
}

public DbSet<Warehouse> Warehouses { get; set; }

public void Seed()
{
Add(new Warehouse
{
WarehouseCode = "W001",
DestinationCountries =
{
new WarehouseDestinationCountry { Id = "1", CountryCode = "US" },
new WarehouseDestinationCountry { Id = "2", CountryCode = "CA" }
}
});

SaveChanges();
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Warehouse>()
.OwnsMany(x => x.DestinationCountries)
.WithOwner()
.HasForeignKey(x => x.WarehouseCode)
.HasPrincipalKey(x => x.WarehouseCode);
}
}

protected class Warehouse
{
public int Id { get; set; }
public string WarehouseCode { get; set; }
public ICollection<WarehouseDestinationCountry> DestinationCountries { get; set; } = new HashSet<WarehouseDestinationCountry>();
}

protected class WarehouseDestinationCountry
{
public string Id { get; set; }
public string WarehouseCode { get; set; }
public string CountryCode { get; set; }
}

protected class WarehouseModel
{
public string WarehouseCode { get; set; }

public ICollection<string> DestinationCountryCodes { get; set; }
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,16 @@ FROM [Blog24133] AS [b]
LEFT JOIN [Post24133] AS [p0] ON [b].[Id] = [p0].[BlogId]
ORDER BY [b].[Id], [p0].[BlogId]");
}

public override async Task Projecting_correlated_collection_property_for_owned_entity(bool async)
{
await base.Projecting_correlated_collection_property_for_owned_entity(async);

AssertSql(
@"SELECT [w].[WarehouseCode], [w].[Id], [w0].[CountryCode], [w0].[WarehouseCode], [w0].[Id]
FROM [Warehouses] AS [w]
LEFT JOIN [WarehouseDestinationCountry] AS [w0] ON [w].[WarehouseCode] = [w0].[WarehouseCode]
ORDER BY [w].[Id], [w0].[WarehouseCode]");
}
}
}