-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Description
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
:
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