Log a warning with using a non-nullable property by convention for an explicitly optional FK

Open
#19,958 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
csharp, sqlite
Domain
database

Research direction

Start by reproducing the issue's two-entity model with SQLite and the shown fluent configuration, then compare model building with and without the conventionally named ContainerId property. Read the relationship and foreign-key convention behavior involved. Done means the intended optional relationship is either supported with regression coverage or the documented behavior is clarified.

Written by the indexing model from the issue text.

Description

area-model-building customer-reported

When we have two entities:

public class Container
{
    public long InnerId { get; set; }

    public List<Item> Items { get; set; } = default!; // still set to not null during the context initialization
}

public class Item
{
    public long InnerId { get; set; }

    // public long ContainerId { get; set; }

    public Container? Container { get; set; }
}

if we configure the model as follows:

modelBuilder.Entity<Container>
    .HasKey(container => container.InnerId);
modelBuilder.Entity<Item>
    .HasKey(item => item.InnerId);
modelBuilder.Entity<Item>
    .HasOne(item => item.Container)
    .WithMany(container => container!.Items);

we expect the model to allow one or less Container to be linked to a single Item, which is explicitly defined by the nullable reference type of Item.Container property. This works, the nullable integer attribute CountainerInnerId is generated in the database to support the desired relation.

If we uncomment public long ContainerId { get; set; }, EF Core finds out that the not nullable property with the "conventional" (<principal entity name>Id) name exists and generates the model that links any Item to exactly one container. Even adding IsRequired(false) does not prevent EF from doing it.

I wasn't able to understand whether it is the by-design behavior or a bug by reading the documentation, but, IMHO, it seems very counter-intuitive that the existence of conventionally named property completely "overrides" manual fluent configuration. I was expecting the shadow foreign key to be introduced to satisfy the desired nullable semantics.

EF Core version: 3.1.1
Database provider: tested it with SQLite (Microsoft.EntityFrameworkCore.Sqlite 3.1.1)
Operating system: Windows 10 Pro 64 bit
IDE: VSCode

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.