Skip to content
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
29 changes: 29 additions & 0 deletions src/Mapster.EFCore.Tests/EFCoreTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Mapster.EFCore.Tests
{
Expand Down Expand Up @@ -45,6 +46,34 @@ public void TestFindObject()
first.Grade.ShouldBe(Grade.F);
}

[TestMethod]
public async Task TestFindSingleObjectUsingProjectToType()
{
var options = new DbContextOptionsBuilder<SchoolContext>()
.UseInMemoryDatabase(Guid.NewGuid().ToString("N"))
.Options;
var context = new SchoolContext(options);
DbInitializer.Initialize(context);

var mapsterInstance = new Mapper();

var query = context.Students.Where(s => s.ID == 1);

async Task<StudentDto> FirstExecute() =>
await mapsterInstance.From(query)
.ProjectToType<StudentDto>()
.FirstOrDefaultAsync();

await Should.NotThrowAsync(async () =>
{
var first = await FirstExecute();

first.ShouldNotBeNull();
first.ID.ShouldBe(1);
first.LastName.ShouldBe("Alexander");
});
}

[TestMethod]
public void MapperInstance_From_OrderBy()
{
Expand Down
10 changes: 10 additions & 0 deletions src/Mapster.EFCore/MapsterQueryable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,21 @@ public TResult ExecuteAsync<TResult>(Expression expression, CancellationToken ca
{
var enumerable = ((IAsyncQueryProvider)_provider).ExecuteAsync<TResult>(expression, cancellationToken);
var enumerableType = typeof(TResult);
if (!IsAsyncEnumerableType(enumerableType))
{
return enumerable;
}
var elementType = enumerableType.GetGenericArguments()[0];
var wrapType = typeof(MapsterAsyncEnumerable<>).MakeGenericType(elementType);
return (TResult) Activator.CreateInstance(wrapType, enumerable, _builder);
}

private static bool IsAsyncEnumerableType(Type type)
{
return type.GetInterfaces()
.Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IAsyncEnumerable<>));
}

public IAsyncEnumerable<TResult> ExecuteEnumerableAsync<TResult>(Expression expression, CancellationToken cancellationToken = default)
{
var enumerable = ((IAsyncQueryProvider)_provider).ExecuteAsync<IAsyncEnumerable<TResult>>(expression, cancellationToken);
Expand Down
Loading