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

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.

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

RequiredField = table.Column<string>(nullable: false)

migration on EF Core 3.0.0 preview 7

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

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 20
  • Comments: 15 (7 by maintainers)

Most upvoted comments

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

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

@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

@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

The issue has been discussed in detail in #16552

@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

@ftathiago It is closed because it is a duplicate of #12100. This is fixed in 5.0. If you are seeing otherwise, then please open a new issues and attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate.

Why this issue was closed, since still not working at efcore 5.0.8?

@StefanSimion Have you found out a solution?