Log a warning with using a non-nullable property by convention for an explicitly optional FK
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
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
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from dotnet/efcore
-
Difficulty 4/5 3-5 days Newbie friendliness 55/100
-
customer-reported
Difficulty 5/5 Over a week Newbie friendliness 38/100
-
area-cosmos area-vector-search
Difficulty 5/5 Over a week Newbie friendliness 25/100
-
area-cosmos
Difficulty 5/5 Over a week Newbie friendliness 25/100
-
area-tools needs-design
Difficulty 4/5 3-5 days Newbie friendliness 25/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
:watch: Not Triaged 11.0 fundamentals/subsvc
Difficulty 2/5 1-3 hours Newbie friendliness 92/100
dotnet/AspNetCore.Docs#37699 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
SubtitleEdit/subtitleedit#15108 · 1 comment ·
-
area/docs-content Bug pulumi/docs
Difficulty 1/5 1-3 hours Newbie friendliness 94/100
-
agentic-workflows untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 76/100