-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Open
Labels
Description
Bug description
When calling context.Entry(...).OriginalValues.ToObject() I am getting an exception
System.InvalidCastException: Unable to cast object of type 'Job' to type 'Error'.
This occurs in 10.0.2 (repro below and https://github.com/blueghostuk/EF10ComplexTypeError) , but not in 10.0.1 (repro in https://github.com/blueghostuk/EF10ComplexTypeError/tree/10.0.1)
Exception is thrown for Jobs where Error or InnerError are null
- "Job with No Error"
- "Job with Error Only"
No exception is thrown when Error and InnerError are not null, i.e. "Job with Error + Inner Error"
Your code
// See https://aka.ms/new-console-template for more information
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using ILoggerFactory factory = LoggerFactory.Create(builder => builder.AddConsole());
var logger = factory.CreateLogger("Program");
Console.WriteLine("Hello, World!");
await using var context = new JobContext();
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();
// this record will throw an exception
context.Jobs.Add(new() { Id = Guid.NewGuid(), Name = "Job with No Error" });
// this record will be ok
context.Jobs.Add(new()
{
Id = Guid.NewGuid(),
Name = "Job with Error + Inner Error",
Error = new()
{
Code = "500",
Message = "Internal Server Error",
InnerError = new()
{
Code = "501",
Message = "Not Implemented"
}
}
});
// this record will throw an exception
context.Jobs.Add(new()
{
Id = Guid.NewGuid(),
Name = "Job with Error only",
Error = new()
{
Code = "400",
Message = "Bad Request"
}
});
await context.SaveChangesAsync();
context.ChangeTracker.Clear();
var jobs = await context.Jobs.ToListAsync();
foreach (var job in jobs)
{
logger.LogInformation("Processing Job: {Job}", job);
try
{
var original = context.Entry(job).OriginalValues.ToObject() as Job;
logger.LogInformation("Retrieved Original Values for Job: {Job}", job);
}
catch (Exception ex)
{
logger.LogError(ex, "Error retrieving original values for Job: {Job}", job);
}
}
Console.WriteLine("Completed");
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
public class JobContext : DbContext
{
public DbSet<Job> Jobs { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseSqlServer("Data Source=.;Initial Catalog=ComplextTest;Integrated Security=True;MultipleActiveResultSets=True;TrustServerCertificate=true;")
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging();
protected override void OnModelCreating(ModelBuilder modelBuilder)
=> modelBuilder.Entity<Job>().ComplexProperty(x => x.Error, x => x.ToJson());
}
public class Job
{
public Guid Id { get; set; }
public required String Name { get; set; }
public Error? Error { get; set; }
public override string ToString()
{
return $"Job(Id={Id}, Name={Name})";
}
}
public class Error
{
public required string Code { get; set; }
public required string Message { get; set; }
public Error? InnerError { get; set; }
}Stack traces
System.InvalidCastException: Unable to cast object of type 'Job' to type 'Error'.
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.ArrayPropertyValues.ToObject()
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntryPropertyValues.ToObject()
at Program.<Main>$(String[] args) in D:\source\ConsoleApp1\Program.cs:line 50
Verbose output
EF Core version
10.0.2
Database provider
Microsoft.EntityFrameworkCore.SqlServer
Target framework
.NET 10.0
Operating system
Windows 11
IDE
VS 2026 Pro 18.1.1
Reactions are currently unavailable