EF Core query #4021
-
I have an existing .NET Framework 4.8 WebAPI project using AutoMapper v10.1.1 and EF Core 3.1, which I am upgrading to .NET 6, AutoMapper 11.0.1, and EF Core 6.0.6. The source model contains an owned entity: public class Settings
{
public int Id { get; set; }
...
public AdditionalSettings Additional { get; set; }
}
[Owned]
public class AdditionalSettings
{
public bool Foo { get; set; }
...
}
...
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Settings>().OwnsOne(s => s.Additional);
} CREATE TABLE Settings
(
Id int IDENTITY(1, 1) NOT NULL,
...
Additional_Foo bit NULL,
...
); The target model flattens the data: public class SettingsDto
{
public int Id { get; set; }
public bool Foo { get; set; }
}
...
configuration.CreateMap<Settings, SettingsDto>()
.ForMember(s => s.Foo, o => o.MapFrom(s => s.Additional.Foo)); I then use SettingsDto result = context.Set<Settings>()
.ProjectTo<SettingsDto>(configurationProvider)
.First(s => s.Id == 42); In the .NET Framework version, this works as expected. If none of the owned entity columns are set, the In the .NET 6 version, if none of the owned entity columns are set, I get an exception:
To resolve the problem, I had to cast the configuration.CreateMap<Settings, SettingsDto>()
.ForMember(s => s.Foo, o => o.MapFrom(s => (bool?)s.Additional.Foo)); I couldn't see this change in behaviour documented anywhere. And I'm not entirely sure whether it's AutoMapper or EF Core which has caused it. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Check the execution plan. Compare it bettween the two versions. If it's the same, it's EF Core. |
Beta Was this translation helpful? Give feedback.
-
The generated queries are different. However, I suspect that's more due to improvements in EF Core. .NET Framework + EF Core 3.1 + AutoMapper 10.1:SELECT TOP(1) [c].[Id], ..., [t4].[Foo] AS [Foo]
FROM [dbo].[Settings] AS [c]
LEFT JOIN (
SELECT [c5].[Id], [c5].[Foo]
FROM [dbo].[Settings] AS [c5]
WHERE [c5].[Foo] IS NOT NULL
) AS [t4] ON [c].[Id] = [t4].[Id]
WHERE [c].[Id] = @__id_0 .NET 6 + EF Core 6.0.6 + AutoMapper 11.0.1:Original query:SELECT TOP(1) [c].[Id], ..., [c].[Foo] AS [Foo]
FROM [dbo].[Settings] AS [c]
WHERE [c].[Id] = @__id_0 Fixed query:SELECT TOP(1) [c].[Id], ... COALESCE([c].[Foo], CAST(0 AS bit)) AS [Foo]
FROM [dbo].[Settings] AS [c]
WHERE [c].[Id] = @__id_0 |
Beta Was this translation helpful? Give feedback.
Check the execution plan. Compare it bettween the two versions. If it's the same, it's EF Core.