Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constructor calls when using inheritance with owned entities causes exceptions #31353

Closed
Jejuni opened this issue Jul 25, 2023 · 0 comments · Fixed by #32813
Closed

Constructor calls when using inheritance with owned entities causes exceptions #31353

Jejuni opened this issue Jul 25, 2023 · 0 comments · Fixed by #32813
Labels
area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported Servicing-approved type-bug
Milestone

Comments

@Jejuni
Copy link

Jejuni commented Jul 25, 2023

Description and Code

When using owned entities it is fairly normal to use some sort of constructor validation, like throwing ArgumentNullException for null values.
When an owned type like this is used in an inheritance hierarchy on a child EfCore tries to call the constructor with null values for some reason when the parent DbSet is queried. This then leads to a ArgumentNullException.
This only happens when entities of another type that actually have NULL values in their columns are also present in the table.

Repro

A repo for the problem can be found here: https://github.com/Jejuni/EfCoreInheritanceProblem

Replace the SqlServer connection string in MyDbContext with a valid one for your machine.

Code

Given these entities:

public class Name
{
    public Name(string firstName, string lastName)
    {
        FirstName = firstName ?? throw new ArgumentNullException(nameof(firstName));
        LastName = lastName ?? throw new ArgumentNullException(nameof(lastName));
    }

    public string FirstName { get; }
    public string LastName { get; }
}


public abstract class Parent
{
    public Guid Id { get; set; }
}

public class ChildOne : Parent
{
    public int Number { get; set; }
}

public class ChildTwo : Parent
{
    public Name Name { get; set; }
}

and this configuration:

modelBuilder.Entity<Parent>(b =>
        {
            b.HasDiscriminator<string>("Discriminator")
                .HasValue<ChildOne>("one")
                .HasValue<ChildTwo>("two");
        });

        modelBuilder.Entity<ChildTwo>(b =>
        {
            b.OwnsOne(x => x.Name, nameBuilder =>
            {
                nameBuilder.Property(x => x.FirstName);
                nameBuilder.Property(x => x.LastName);
            });
        });

leads to an exception when the following code is run:

await using (var ctx = new MyDbContext())
{
    await ctx.Database.EnsureDeletedAsync();
    await ctx.Database.EnsureCreatedAsync();
}

await using (var ctx2 = new MyDbContext())
{
    ctx2.ChildOnes.Add(new ChildOne { Number = 2 });

    await ctx2.SaveChangesAsync();
}

await using (var ctx3 = new MyDbContext())
{
    var childOnes = await ctx3.ChildOnes.ToListAsync();
    var childTwos = await ctx3.ChildTwos.ToListAsync();
    // NEXT LINE CAUSES EXCEPTION
    var parents = await ctx3.Parents.ToListAsync();
}

Include stack traces

Stack trace shows the code thrown from user error during execution of SingleQueryingEnumerable:

System.ArgumentNullException
  HResult=0x80004003
  Message=Value cannot be null. (Parameter 'firstName')
  Source=EfCoreInheritanceProblem
  StackTrace:
   at EfCoreInheritanceProblem.Name..ctor(String firstName, String lastName) in Whatever\EfCoreInheritanceProblem\EfCoreInheritanceProblem\Entities.cs:line 7
   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.<MoveNextAsync>d__20.MoveNext()

Include provider and version information

EF Core version:
Database provider: Microsoft.EntityFrameworkCore.SqlServer, 7.0.9
Target framework: .NET 7.0
Operating system: Windows 11
IDE: Visual Studio 2022 17.6.5

@ajcvickers ajcvickers self-assigned this Jul 27, 2023
@ajcvickers ajcvickers added this to the Backlog milestone Jul 27, 2023
@ajcvickers ajcvickers modified the milestones: Backlog, 8.0.x Jan 13, 2024
ajcvickers added a commit that referenced this issue Jan 14, 2024
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Jan 14, 2024
@ajcvickers ajcvickers reopened this Jan 22, 2024
@ajcvickers ajcvickers modified the milestones: 8.0.x, 8.0.3 Feb 2, 2024
@ajcvickers ajcvickers removed their assignment Aug 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported Servicing-approved type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants