Skip to content

Bug Report: ArgumentException - Static property requires null instance #126

@BilalSakaiTech

Description

@BilalSakaiTech

Description
After successfully upgrading to .NET 9 and EF Core 9.0.2, I'm encountering an ArgumentException when using Detached Mapper's MapAsync() method. The error indicates an issue with static properties during the mapping process.
Error Message
System.ArgumentException: Static property requires null instance, non-static property requires non-null instance. (Parameter 'expression')
at System.Linq.Expressions.Expression.Property(Expression expression, PropertyInfo property)
at Detached.Mappers.Types.Class.ClassTypeFactory.CreateMember(ClassType classType, PropertyInfo propInfo)
at Detached.Mappers.Types.Class.ClassTypeFactory.Create(MapperOptions options, Type clrType)
at Detached.Mappers.Options.MapperOptions.GetTypeConfiguration(Type clrType)
at Detached.Mappers.EntityFramework.Loaders.LoaderQueryFactory.GetIncludes(TypePair typePair)
at Detached.Mappers.EntityFramework.Loaders.LoaderQueryFactory.Create(Type sourceClrType, Type targetClrType)
at Detached.Mappers.EntityFramework.Loaders.Loader.LoadAsync(DbContext dbContext, Type entityType, Object entityOrDto)
at Detached.Mappers.EntityFramework.EntityMapper.MapAsync[TEntity](DbContext dbContext, Object entityOrDto, MapParameters parameters)

Environment

Detached Mapper Version: 9.0.2
Entity Framework Core Version: 9.0.2
Npgsql.EntityFrameworkCore.PostgreSQL Version: 9.0.2
.NET Version: .NET 9.0
Database Provider: PostgreSQL (Npgsql)

Code Sample

Entity Model (Category.cs):

csharpusing System.ComponentModel.DataAnnotations;
using Detached.Annotations;
using Microsoft.EntityFrameworkCore;

namespace DetachedMapperDemo.Api.Models;

public class Category
{
public int Id { get; set; }

[MaxLength(50)]
public string Name { get; set; } = null!;

public bool IsActive { get; set; } = true;

public DateOnly CreatedAt { get; set; }

public DateOnly? UpdatedAt { get; set; }

}

ViewModel (CategorySaveVM.cs):
csharppublic class CategorySaveVM
{
public int Id { get; set; }

[MaxLength(50)]
public string Name { get; set; } = null!;

public DateOnly CreatedAt { get; set; }

public DateOnly? UpdatedAt { get; set; }

}

Service Code (CategoryService.cs):

csharppublic async Task SaveCategory(CategorySaveVM vm)
{
var category = await context.MapAsync(vm);
context.Categories.Add(category);
await context.SaveChangesAsync();

return category.Id;

}

Program.cs Configuration:

csharpusing Microsoft.EntityFrameworkCore;
using DetachedMapperDemo.Api.Data;
using Detached.Mappers.EntityFramework;
using DetachedMapperDemo.Api.Services;

var builder = WebApplication.CreateBuilder(args);

builder.Services
.AddDbContext(options =>
options.UseNpgsql(
builder.Configuration.GetConnectionString("DefaultConnection"),
sqlServerOptions =>
{
sqlServerOptions.EnableRetryOnFailure(
maxRetryCount: 5,
maxRetryDelay: TimeSpan.FromSeconds(30),
errorCodesToAdd: null
);
}
)
.UseMapping()
);

builder.Services.AddControllers();
builder.Services.AddOpenApi();
builder.Services.AddTransient();

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}

app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();

Steps to Reproduce

Create a simple entity model (Category) with no static properties
Create a corresponding ViewModel (CategorySaveVM) with matching properties
Configure DbContext with .UseMapping() extension
Attempt to use context.MapAsync(viewModel)
Exception is thrown during the mapping process

Expected Behavior
The mapper should successfully map the ViewModel to the Entity without throwing an ArgumentException related to static properties.
Actual Behavior
An ArgumentException is thrown with the message "Static property requires null instance, non-static property requires non-null instance" even though neither the entity nor the ViewModel contain any static properties.
Additional Context

This issue occurs after upgrading from .NET 8 to .NET 9 and matching EF Core versions
The entity and ViewModel models contain no static properties
The error seems to be triggered during the LoaderQueryFactory.GetIncludes() method execution
The error occurs in the ClassTypeFactory.CreateMember() method when it tries to create a property expression

Investigation Notes
The stack trace suggests that Detached Mapper is attempting to create a LINQ expression for a property it believes is static, but this doesn't match the actual model definitions. This might be:

A false positive detection of static properties in .NET 9
An issue with how .NET 9 exposes property metadata
A problem with EF Core 9.0.2's model metadata that Detached Mapper is interpreting incorrectly

Possible Causes

Changes in how .NET 9 handles property reflection
Changes in EF Core 9.0.2's metadata API
Detached Mapper 9.0.2 may need updates to handle .NET 9 / EF Core 9.0.2 property metadata correctly

Workaround

Currently using manual mapping as a temporary solution:
csharppublic async Task SaveCategory(CategorySaveVM vm)
{
var category = new Category
{
Name = vm.Name,
CreatedAt = vm.CreatedAt,
UpdatedAt = vm.UpdatedAt,
IsActive = true
};

context.Categories.Add(category);
await context.SaveChangesAsync();

return category.Id;

}

Questions:

Is this a known issue with .NET 9 / EF Core 9.0.2?
Are there any configuration options to help Detached Mapper handle .NET 9's property reflection differently?
Could this be related to how DateOnly properties are handled in .NET 9?

Thank you for your assistance!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions