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

EntityTypeBuilder: .IsRequired() is ignored if used after .OnDelete() #23555

Closed
gukoff opened this issue Dec 2, 2020 · 2 comments
Closed

EntityTypeBuilder: .IsRequired() is ignored if used after .OnDelete() #23555

gukoff opened this issue Dec 2, 2020 · 2 comments
Labels
area-model-building closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported regression Servicing-approved type-bug
Milestone

Comments

@gukoff
Copy link

gukoff commented Dec 2, 2020

Problem

When configuring a non-nullable foreign key with FluentAPI, .isRequired() is ignored when called after .OnDelete().

Code

There are 2 models, connected by a 1-to-many relationship:

class Country {
   public long Id { get; set; }
}

class City {
   public long Id { get; set; }
   public Country Country { get; set; }
}

I'd like every city to belong to some country. This means, make the foreign key City.CountryId non-nullable.

Now, this doesn't work:

EntityTypeBuilder<City> builder;  // initialized

builder
.HasOne(city => city.Country)
.WithMany(country => country.Cities)
.OnDelete(DeleteBehavior.Restrict)
.IsRequired();   // after OnDelete

But this does:

EntityTypeBuilder<City> builder;  // initialized

builder
.HasOne(city => city.Country)
.WithMany(country => country.Cities)
.IsRequired()  // before OnDelete
.OnDelete(DeleteBehavior.Restrict);

The only difference is the order of IsRequired() and OnDelete().

The first (buggy) snippet generates a database migration with a nullable foreign key:

CountryId = table.Column<long>(type: "bigint", nullable: true)

And the second doesn't:

CountryId = table.Column<long>(type: "bigint", nullable: false)

Diagnostics

EF Core version: 5.0.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 3.1
Operating system: MacOS (reproducible on Windows too)
IDE: command line

@migellars
Copy link

Hi, @gukoff but _I'd like every city to belong to some country. __ a country should have many cities

@ajcvickers
Copy link
Contributor

@AndriySvyryd This repros for me and is a regression from 3.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-model-building closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported regression Servicing-approved type-bug
Projects
None yet
Development

No branches or pull requests

4 participants