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
32 changes: 32 additions & 0 deletions src/Mapster.EFCore.Tests/EFCoreTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,41 @@ public void NotMemberNameEFCoreProjectToType_not_Error()

}

/// <summary>
/// https://github.com/MapsterMapper/Mapster/issues/881
/// </summary>
[TestMethod]
public void RecordsEFCoreProjectToType_not_Error()
{
var options = new DbContextOptionsBuilder<SchoolContext>()
.UseInMemoryDatabase(Guid.NewGuid().ToString("N"))
.Options;
var context = new SchoolContext(options);
DbInitializer.Initialize(context);


Should.NotThrow(() =>
{
var query = context.Students
.Include(x => x.Enrollments.OrderByDescending(x => x.StudentID).Take(1))
.EFCoreProjectToType<StudentRecordDto>();

var first = query.First();

first.Enrollments.Count.ShouldBe(1);
first.LastName.ShouldBe("Alexander");

});

}
}


public record StudentRecordDto
{
public int ID { get; set; }
public string LastName { get; set; }
public ICollection<EnrollmentItemDto> Enrollments { get; set; }
}

public class StudentDto
Expand Down
2 changes: 1 addition & 1 deletion src/Mapster/Adapters/RecordTypeAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

protected override bool CanMap(PreCompileArgument arg)
{
return arg.DestinationType.IsRecordType();
return arg.DestinationType.IsRecordType() && arg.MapType != MapType.Projection;
}

protected override bool CanInline(Expression source, Expression? destination, CompileArgument arg)
Expand Down Expand Up @@ -51,7 +51,7 @@
}


return RecordInlineExpression(source, destination, arg, installExpr); // Activator field when not include in public ctor

Check warning on line 54 in src/Mapster/Adapters/RecordTypeAdapter.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.
}

private Expression? RecordInlineExpression(Expression source, Expression? destination, CompileArgument arg, Expression installExpr)
Expand Down Expand Up @@ -101,7 +101,7 @@
binEx.Right is ConstantExpression { Value: null })
adapt = condEx.IfFalse;
}
var destinationCompareNull = Expression.Equal(destination, Expression.Constant(null, destination.Type));

Check warning on line 104 in src/Mapster/Adapters/RecordTypeAdapter.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
var sourceCondition = Expression.NotEqual(member.Getter, Expression.Constant(null, member.Getter.Type));
var destinationCanbeNull = Expression.Condition(destinationCompareNull, member.DestinationMember.Type.CreateDefault(), member.DestinationMember.GetExpression(destination));
adapt = Expression.Condition(sourceCondition, adapt, destinationCanbeNull);
Expand Down Expand Up @@ -152,7 +152,7 @@
contructorMembers.Any(x => string.Equals(x.Name, member.Name, StringComparison.InvariantCultureIgnoreCase)))
continue;

lines.Add(Expression.Bind((MemberInfo)member.Info, Expression.MakeMemberAccess(destination, (MemberInfo)member.Info)));

Check warning on line 155 in src/Mapster/Adapters/RecordTypeAdapter.cs

View workflow job for this annotation

GitHub Actions / build

Converting null literal or possible null value to non-nullable type.

Check warning on line 155 in src/Mapster/Adapters/RecordTypeAdapter.cs

View workflow job for this annotation

GitHub Actions / build

Converting null literal or possible null value to non-nullable type.
}

return lines;
Expand Down Expand Up @@ -223,9 +223,9 @@

if (arg.MapType == MapType.MapToTarget)
{
var var2Param = ClassConverterContext.Members.Where(x => x.DestinationMember.Name == member.DestinationMember.Name).FirstOrDefault();

Check warning on line 226 in src/Mapster/Adapters/RecordTypeAdapter.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Expression destMemberVar2 = var2Param.DestinationMember.GetExpression(var2Param.Destination);

Check warning on line 228 in src/Mapster/Adapters/RecordTypeAdapter.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'source' in 'Expression IMemberModelEx.GetExpression(Expression source)'.
var ParamLambdaVar2 = destMemberVar2;
if(member.DestinationMember.Type.IsRecordType())
ParamLambdaVar2 = arg.Context.Config.CreateMapInvokeExpressionBody(member.Getter.Type, member.DestinationMember.Type, destMemberVar2);
Expand Down
2 changes: 1 addition & 1 deletion src/Mapster/Mapster.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<AssemblyOriginatorKeyFile>Mapster.snk</AssemblyOriginatorKeyFile>
<IsPackable>true</IsPackable>
<RootNamespace>Mapster</RootNamespace>
<Version>10.0.3</Version>
<Version>10.0.4-a</Version>
<Nullable>enable</Nullable>
<NoWarn>1701;1702;8618</NoWarn>
</PropertyGroup>
Expand Down
Loading