Better exception when using a stale builder

Open
#13,519 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
25/100
Issue type
Bug
Clarity
Needs clarification
Activity status
Stale
Tech stack
csharp
Domain
backend, database

Research direction

Start with the OnModelCreating repro in the issue, especially the owned-entity Property call that throws NullReferenceException. Trace the exception to determine whether the stale builder scenario is reproducible and what clearer failure should replace it; done means the reported configuration no longer produces an unexplained internal NullReferenceException.

Written by the indexing model from the issue text.

Description

area-model-building customer-reported

Moved from https://github.com/aspnet/EntityFrameworkCore/issues/12954 filed by @raffaeler

@raffaeler commented 2 days ago
@smitpatel At the end the problem was a mix of two issues:

the message coming from the exception is not clear enough to understand the real origin of the problem

@AndriySvyryd commented a day ago
@raffaeler Calling ToTable on the owned types is a workaround for your issue, it shouldn't be needed, that's why it's not documented.

@raffaeler commented a day ago
@AndriySvyryd ok. But without it I could not make it work. I am not sure whether @smitpatel test cover the case when PhysicalSchema was initially specified by hand to the entity (and thus not propagated to the Owned Type)

@smitpatel commented a day ago
@raffaeler - If you look at the code I posted, I have configured the owner type to map to different schema using ToTable call and yet it is propagated properly to owned entities. The code in SharedTableConvention also does processing for table and schema both. Hence the assumption that schema is not getting propagated is incorret.
At this stage we will need repro to investigate further else there does not seem to be any issue in codebase or documentation.

@raffaeler commented 12 hours ago
@smitpatel I see but there is a huge difference between my code and yours.
I have to manually specify all the mappings because in my case I don't know the entities. Therefore I have to use reflection to map them.
Beyond the fact I created new extension methods to use reflection objects such as PropertyInfo, I manually add every single property without letting the conventions decide.

That said, I tried to add to your code the manual mappings, but I get an internal NullReferenceException:

var entityBuilder = modelBuilder.Entity<Blog>().ToTable("Blogging", "dbo");
var ownershipBuilder1 = modelBuilder.Entity<Blog>().OwnsOne(e => e.HomeAddress);
var ownershipBuilder2 = modelBuilder.Entity<Blog>().OwnsOne(e => e.WorkAddress);
var propertyBuilder = ownershipBuilder1.Property<Address>("City"); // explodes internally in EF

Let's start from this ... do you see that?
Thanks

@smitpatel commented 3 hours ago
var propertyBuilder = ownershipBuilder1.Property("City")
Don't forget that the type of Property should match up.
The cause of issue is more likely that you are specifying table manually but not the schema when configuring owned entities.

@raffaeler commented 2 hours ago •
@smitpatel I am now using your example and I did not modify the entities you defined, so string is correct.
I also tried this statement:

var propertyBuilder = ownershipBuilder1.Property("City");

but I still get a NullReferenceException. At home I did not configure the source code stepping so I am not understanding why this exception get thrown. (In any case I don't expect EFCore throwing a NullReferenceException).

For clarity this is the OnModelCreating method:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    // Configure model
    var entityBuilder = modelBuilder.Entity<Blog>().ToTable("Blogging", "Custom");
    var ownershipBuilder1 = modelBuilder.Entity<Blog>().OwnsOne(e => e.HomeAddress);
    var ownershipBuilder2 = modelBuilder.Entity<Blog>().OwnsOne(e => e.WorkAddress);

    var propertyBuilder = ownershipBuilder1.Property<Address>("City"); // throws NullReferenceException internally
    propertyBuilder.HasColumnName("City");

    //propertyBuilder = ownershipBuilder1.Property(typeof(Guid), $"Id.Shadow");
    //propertyBuilder.Metadata.IsPrimaryKey();
    //ownershipBuilder1.HasForeignKey($"Id.Shadow");
}
Dominant language
C#
Stars
14.8k
Forks
3.4k
Avg merge
2d 5h
Merged PRs (30d)
134

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/efcore

All issues in dotnet/efcore

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.