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

[Required] attribute is ignored for owned entity type's properties #21141

Closed
arphox opened this issue Jun 5, 2020 · 2 comments
Closed

[Required] attribute is ignored for owned entity type's properties #21141

arphox opened this issue Jun 5, 2020 · 2 comments

Comments

@arphox
Copy link

arphox commented Jun 5, 2020

Hi!

The NPGSQL team said this problem is in EF Core and not in their provider code. (npgsql/efcore.pg#1402)

In Npgsql.EntityFrameworkCore.PostgreSQL version 3.1.4,
I have the following hierarchy:

public class Profile
{
    public int Id { get; set; }

    [Required]
    public string CompanyName { get; set; }

    [Required]
    public ProfileAddress Address { get; set; }
}

[Owned]
public sealed class ProfileAddress
{
    [Required]
    public string Country { get; set; }

    [Required]
    public string AddressLine1 { get; set; }

    public string AddressLine2 { get; set; }
}

and in my DbContext, only Profile is included as a db set:
public DbSet<Profile> Profiles { get; set; }

This causes the provider to generate address data inside the Profile table which is great, but Country and AddressLine1 are nullable while I configured them to be Required.

This is the generated migration part:

migrationBuilder.CreateTable(
    name: "Profiles",
    columns: table => new
    {
        Id = table.Column<int>(nullable: false)
            .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
        CompanyName = table.Column<string>(nullable: false),
        Address_Country = table.Column<string>(nullable: true),
        Address_AddressLine1 = table.Column<string>(nullable: true),
        Address_AddressLine2 = table.Column<string>(nullable: true)
    },
    constraints: table =>
    {
        table.PrimaryKey("PK_Profiles", x => x.Id);
    });

I expect both Address_Country and Address_AddressLine1 to be not nullable, as I just configured. I also tried configuring the same with FluentAPI but that did not work for me as well.

@GertArnold
Copy link

#16943

@ajcvickers
Copy link
Contributor

Duplicate of #12100

@ajcvickers ajcvickers marked this as a duplicate of #12100 Jun 5, 2020
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants