Skip to content

Proxies using Records with a base class broken since .NET 6 #26602

@Jejuni

Description

@Jejuni

Description

When using a record with a base class while having lazy loading proxies enabled any usage of the DbContext will immediately crash with the following TypeLoadException:

image

This exception did not happen before .NET 6 as we've used this kind of code all over our production code base with the expected results.

Repo

A Repo to quickly demonstrate the bug can be found here:
https://github.com/Jejuni/Net6RecordEfCoreProxies

Code

Entities look like this:

public class MyEntity
{
    public Guid Id { get; set; }
    public virtual MyRecord MyRecord { get; init; }
}

public record MyRecord : MyBaseRecord
{
    public int MyInt { get; init; }
}

public record MyBaseRecord
{
}

DbContext is configured to have MyRecord be an owned entity via OwnsOne.

Crashing code:

var ctx = new TestDbContext();
ctx.Database.EnsureDeleted();
ctx.Database.EnsureCreated();

However, the exception is also thrown when MyRecord is configured explicitly or per convention as a HasOne.
The exception is not thrown when a class is used in place of a record

Exception:

System.TypeLoadException: 'Return type in method 'Castle.Proxies.MyRecordProxy.<Clone>$()' on type 
'Castle.Proxies.MyRecordProxy' from assembly 
'DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' 
is not compatible with base type method 'Net6RecordEfCoreProxies.MyRecord.<Clone>$()'.'

Include provider and version information

EF Core version: 6.0.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer / InMemory / Probably any other one as well
Target framework: .NET 6.0
Operating system: Windows 10
IDE: Visual Studio 2022 17.0.0

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions