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

Nullable property is required if there is another not-nullable string property in model #16760

Closed
SonicGD opened this issue Jul 26, 2019 · 2 comments · Fixed by #16781
Closed
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Milestone

Comments

@SonicGD
Copy link

SonicGD commented Jul 26, 2019

Hello. After i upgraded to preview7 from preview5 some of my queries started to throw exceptions cause of null values in database for nullable properties. I reproduced this in test project and there is something strange. I have Nullable reference types enabled.

Steps to reproduce

Define model class

public class Bar
{
    [Key] public Guid Id { get; set; }
    public Guid? EmptyId { get; set; }
}

Now generate migration:

migrationBuilder.CreateTable(
    name: "Bars",
    columns: table => new
    {
        Id = table.Column<Guid>(nullable: false),
        EmptyId = table.Column<Guid>(nullable: true)
    },
    constraints: table =>
    {
        table.PrimaryKey("PK_Bars", x => x.Id);
    });

Property EmptyId has nullable: true as it should.

Now let's create another model with additional sting property

public class Foo
{
    [Key] public Guid Id { get; set; }
    public Guid? EmptyId { get; set; }
    public string SomeString { get; set; }
}

and generate new migration

migrationBuilder.CreateTable(
    name: "Foos",
    columns: table => new
    {
        Id = table.Column<Guid>(nullable: false),
        EmptyId = table.Column<Guid>(nullable: false),
        SomeString = table.Column<string>(nullable: false)
    },
    constraints: table =>
    {
        table.PrimaryKey("PK_Foos", x => x.Id);
    });

Here property EmptyId has nullable: false for some reasons.

If we change SomeString to nullable string

public class FooBar
{
    [Key] public Guid Id { get; set; }
    public Guid? EmptyId { get; set; }
    public string? SomeString { get; set; }
}

then both properties are correctly nullable:

migrationBuilder.CreateTable(
    name: "FooBars",
    columns: table => new
    {
        Id = table.Column<Guid>(nullable: false),
        EmptyId = table.Column<Guid>(nullable: true),
        SomeString = table.Column<string>(nullable: true)
    },
    constraints: table =>
    {
        table.PrimaryKey("PK_FooBars", x => x.Id);
    });

and if we move EmptyId to base class:

public abstract class BaseBaz
{
    public Guid? EmptyId { get; set; }
}

public class Baz : BaseBaz
{
    [Key] public Guid Id { get; set; }
    public string SomeString { get; set; } = default!;
}

it will be nullable:

migrationBuilder.CreateTable(
    name: "Bazes",
    columns: table => new
    {
        Id = table.Column<Guid>(nullable: false),
        EmptyId = table.Column<Guid>(nullable: true),
        SomeString = table.Column<string>(nullable: false)
    },
    constraints: table =>
    {
        table.PrimaryKey("PK_Bazes", x => x.Id);
    });

Further technical details

EF Core version: 3.0.0-preview7.19362.6
Database Provider: Microsoft.EntityFrameworkCore.SqlServer and Npgsql.EntityFrameworkCore.PostgreSQL
Operating system: Windows 10 18941
IDE: dotnet cli

@SonicGD SonicGD changed the title Nullable reference property is required if there is another not-nullable string poperty in model Nullable property is required if there is another not-nullable string poperty in model Jul 26, 2019
@SonicGD SonicGD changed the title Nullable property is required if there is another not-nullable string poperty in model Nullable property is required if there is another not-nullable string property in model Jul 26, 2019
@ajcvickers
Copy link
Member

@roji Looks like our first bug with nullable reference types!

@roji
Copy link
Member

roji commented Jul 26, 2019

@SonicGD thanks for reporting! Your sample indeed exposed some bugs in the nullability handling, have submitted a PR with the fixes. Hopefully you can continue testing with the preview8 nightlies once that's merged!

roji added a commit that referenced this issue Jul 26, 2019
@roji roji added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Jul 26, 2019
@ajcvickers ajcvickers modified the milestones: 3.0.0, 3.0.0-preview8 Jul 29, 2019
@ajcvickers ajcvickers modified the milestones: 3.0.0-preview8, 3.0.0 Nov 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants