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

Why does my non-nullable navigation property have a nullable backing field? #23588

Closed
gojanpaolo opened this issue Nov 30, 2020 · 2 comments · Fixed by #25831
Closed

Why does my non-nullable navigation property have a nullable backing field? #23588

gojanpaolo opened this issue Nov 30, 2020 · 2 comments · Fixed by #25831
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 type-bug
Milestone

Comments

@gojanpaolo
Copy link

I created the following class:

class Post
{
    public Blog Blog { get; set; }
}

but the backing field in the database is BlogId (FK, int, null) instead of not null. I have enabled nullable reference type in my project. Is there anything else I should configure?

Thank you.


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

@ajcvickers
Copy link
Contributor

Note for triage: I am able to reproduce this.

#nullable enable

public class Blog
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class Post
{
    public int Id { get; set; }
    public string Content { get; set; }

    public Blog Blog { get; set; }
}

public class SomeDbContext : DbContext
{
    private static ILoggerFactory ContextLoggerFactory
        => LoggerFactory.Create(b => b.AddConsole().SetMinimumLevel(LogLevel.Information));

    public DbSet<Blog> Blogs { get; set; }
    public DbSet<Post> Posts { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder
            .UseSqlServer(Your.ConnectionString)
            .UseLoggerFactory(ContextLoggerFactory)
            .EnableSensitiveDataLogging();
}

public class Program
{
    public static void Main()
    {
        using (var context = new SomeDbContext())
        {
            context.Database.EnsureDeleted();
            context.Database.EnsureCreated();
        }
    }
}
      CREATE TABLE [Blogs] (
          [Id] int NOT NULL IDENTITY,
          [Name] nvarchar(max) NOT NULL,
          CONSTRAINT [PK_Blogs] PRIMARY KEY ([Id])
      );

      CREATE TABLE [Posts] (
          [Id] int NOT NULL IDENTITY,
          [Content] nvarchar(max) NOT NULL,
          [BlogId] int NULL,
          CONSTRAINT [PK_Posts] PRIMARY KEY ([Id]),
          CONSTRAINT [FK_Posts_Blogs_BlogId] FOREIGN KEY ([BlogId]) REFERENCES [Blogs] ([Id]) ON DELETE NO ACTION
      );

@ajcvickers ajcvickers transferred this issue from dotnet/EntityFramework.Docs Dec 4, 2020
@ajcvickers
Copy link
Contributor

Notes for triage:

  • Putting [Required] on the navigation doesn't help
  • Adding an inverse navigation does

@ajcvickers ajcvickers added this to the 6.0.0 milestone Dec 7, 2020
@AndriySvyryd AndriySvyryd added the verify-fixed This issue is likely fixed in new query pipeline. label Aug 31, 2021
@AndriySvyryd AndriySvyryd added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Sep 1, 2021
@AndriySvyryd AndriySvyryd removed their assignment Sep 1, 2021
@ajcvickers ajcvickers modified the milestones: 6.0.0, 6.0.0-rc2 Sep 2, 2021
@AndriySvyryd AndriySvyryd removed the verify-fixed This issue is likely fixed in new query pipeline. label Sep 29, 2021
@ajcvickers ajcvickers modified the milestones: 6.0.0-rc2, 6.0.0 Nov 8, 2021
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 type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants