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
6 changes: 3 additions & 3 deletions src/Mapster.Core/Utils/ProjectToTypeVisitors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
{
public sealed class TopLevelMemberNameVisitor : ExpressionVisitor
{
public string? MemeberName { get; private set; }
public string? MemberName { get; private set; }

public override Expression Visit(Expression node)
{
if (node == null)
return null;

Check warning on line 13 in src/Mapster.Core/Utils/ProjectToTypeVisitors.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.
switch (node.NodeType)
{
case ExpressionType.MemberAccess:
{
if (string.IsNullOrEmpty(MemeberName))
MemeberName = ((MemberExpression)node).Member.Name;
if (string.IsNullOrEmpty(MemberName))
MemberName = ((MemberExpression)node).Member.Name;

return base.Visit(node);
}
Expand All @@ -33,7 +33,7 @@
public override Expression Visit(Expression node)
{
if (node == null)
return null;

Check warning on line 36 in src/Mapster.Core/Utils/ProjectToTypeVisitors.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.
switch (node.NodeType)
{
case ExpressionType.Quote:
Expand Down
31 changes: 31 additions & 0 deletions src/Mapster.EFCore.Tests/EFCoreTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,37 @@ public void MergeIncludeWhenUsingEFCoreProjectToType()
first.Enrollments.Count.ShouldBe(1);
first.LastName.ShouldBe("Alexander");
}

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

var config = new TypeAdapterConfig();

config
.NewConfig<Student, StudentDto>()
.Map(dest => dest.LastName, src => src.GetLastName());

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

}




}

public class StudentDto
Expand Down
1 change: 1 addition & 0 deletions src/Mapster.EFCore.Tests/Models/Student.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ public class Student
public DateTime EnrollmentDate { get; set; }

public ICollection<Enrollment> Enrollments { get; set; }
public string GetLastName () { return LastName; }
}
}
2 changes: 1 addition & 1 deletion src/Mapster.EFCore/EFCoreExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public override Expression Visit(Expression node)
var memberv = new TopLevelMemberNameVisitor();
memberv.Visit(item);

IncludeExpression.TryAdd(memberv.MemeberName, item);
IncludeExpression.TryAdd(memberv.MemberName, item);
}
}
return base.Visit(node);
Expand Down
2 changes: 1 addition & 1 deletion src/Mapster/Adapters/BaseClassAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

#region Build the Adapter Model

protected ClassMapping CreateClassConverter(Expression source, ClassModel classModel, CompileArgument arg, Expression? destination = null, bool ctorMapping = false, ClassModel recordRestorMemberModel = null)

Check warning on line 18 in src/Mapster/Adapters/BaseClassAdapter.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
{
var destinationMembers = classModel.Members;
var unmappedDestinationMembers = new List<string>();
Expand Down Expand Up @@ -50,7 +50,7 @@

s.Visit(getter);

if (arg.Settings.ProjectToTypeResolvers.TryGetValue(s.MemeberName, out var match))
if (s.MemberName != null && arg.Settings.ProjectToTypeResolvers.TryGetValue(s.MemberName, out var match))
{
arg.Settings.Resolvers.Add(new InvokerModel
{
Expand Down Expand Up @@ -202,7 +202,7 @@
&& ignore.Condition == null;
}

protected Expression CreateInstantiationExpression(Expression source, ClassMapping classConverter, CompileArgument arg, Expression? destination, ClassModel recordRestorParamModel = null)

Check warning on line 205 in src/Mapster/Adapters/BaseClassAdapter.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
{
var members = classConverter.Members;

Expand Down
Loading