Efcore: [Required]/IsRequired() on properties of owned entities is ignored

Created on 4 Aug 2019  路  13Comments  路  Source: dotnet/efcore

In previous stable EF Core version (2.2.6) required properties of owned entities were marked as not null-able in the migration code. With .NET Core 3.0.0 preview 7 (6, 5 and probably previous versions too) they are marked as null-able (nullable: true).
My tests in this solution demonstrate it.

image

Steps to reproduce

Please take a look OwnedEntityRequired project in this solution and InMemoryTests.OwnedEntityRequired() xUnit test.

```c#
public class DummyModel
{
[Required]
[Key, Column(Order = 0)]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long Id { get; set; }

[Required]
public OwnedModel OwnedModel { get; set; }

}

[Owned]
public class OwnedModel
{
[Column(nameof(RequiredField))]
[Required]
public string RequiredField { get; set; }
}

migration on EF Core 2.2.6
```c#
RequiredField = table.Column<string>(nullable: false)

migration on EF Core 3.0.0 preview 7
c# RequiredField = table.Column<string>(nullable: true)

Further technical details

EF Core version: 3.0.0-preview7.19362.6
Database Provider: any (InMemory cannot be used for test, because it ignores IsRequired())
Operating system: any
IDE: Visual Studio 2019 16.2

closed-duplicate customer-reported

Most helpful comment

Everyone that needs this functionality please vote (:+1:) for https://github.com/aspnet/EntityFrameworkCore/issues/12100

All 13 comments

9005

9005

@smitpatel please read both issues carefully, I'm not using table splitting.
IMO this might be a breaking change, but at the same time it's a regression (just like global filters in EF Core 3.0.0 preview 6+)

The issue has been discussed in detail in #16552

@ajcvickers this is pretty frustrating experience when a bot-like user with no real code on GitHub shuts down your bug reports, you spent hours to polish, by creating demonstrable samples.

I just want to understand how can we get the same functionality we had on 2.2.6 when [Required]/IsRequired()-marked properties are CORRECTLY created as NON-nullable fields in the migration code.

If there is not way to revert new behavior, then it's a bug/regression

@ajcvickers @AndriySvyryd will I be able to restore 2.2.6 behavior with #15607 implemented and by marking owned property itself as [Required]/IsRequired()?

@salaros Yes, #15607 will allow you to configure the columns as non-nullable.

I'm having the same issue with IsRequired() being ignored for owned type properties.
I'm using EF Core 3.0.0-rc1.19456.14 with SQLite database provider.
@salaros @AndriySvyryd can you provide some code example with the fix?

Cheers

@StefanSimion
Have you found out a solution?

@StefanSimion @Mike-Wazowski there is not solution, unfortunately for now EF Core team is busy breaking things right before stable releases, by committing controversial stuff
https://github.com/aspnet/EntityFrameworkCore/issues/16999

I really need this functionality back. (or a workaround for now)

@davidhenley If you need the column to not be nullable you can modify the generated migration.

Everyone that needs this functionality please vote (:+1:) for https://github.com/aspnet/EntityFrameworkCore/issues/12100

Was this page helpful?
0 / 5 - 0 ratings