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

EFCore 6 - NullReferenceException upon EnsureDeleted #26436

Closed
shvmgpt116 opened this issue Oct 22, 2021 · 4 comments
Closed

EFCore 6 - NullReferenceException upon EnsureDeleted #26436

shvmgpt116 opened this issue Oct 22, 2021 · 4 comments
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 regression Servicing-approved type-bug
Milestone

Comments

@shvmgpt116
Copy link

I have a sample application which used to work with EFCore 5 sql server provider. But with EFCore 6 rc2, I am getting null reference exception when executing EnsureDeleted().

Here is the sample application

using System;
using Microsoft.EntityFrameworkCore;
using System.Linq;
using Microsoft.Extensions.Logging;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace EFCoreProject
{
  class Program
  {
    static void Main(string[] args)
    {
      try
      {
        using (var db = new CompanyContext())
        {
          db.Database.EnsureDeleted();
          db.Database.EnsureCreated();
        }
      }
      catch (Exception e)
      {
        Console.WriteLine(e);
      }
    }
  }

  public class CompanyContext : DbContext
  {
    public DbSet<Employee> Employees { get; set; }
    public DbSet<EmployeeDetail> EmployeeDetails { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
      optionsBuilder.UseSqlServer(<connection string>)
        .UseLoggerFactory(_myLoggerFactory);
    }
    public static readonly LoggerFactory _myLoggerFactory = new LoggerFactory(new[] { new Microsoft.Extensions.Logging.Debug.DebugLoggerProvider() });
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
      modelBuilder.Entity<Employee>().HasOne(p => p.EmployeeDetail).WithOne(p => p.Employee).HasForeignKey<EmployeeDetail>(p => p.EmployeeId);
    }
  }

  [Table("Employee")]
  public class Employee
  {
    [Key]
    [Column("EmployeeId")]
    public decimal EmployeeId { get; set; }

    [ForeignKey("EmployeeId")]
    public EmployeeDetail EmployeeDetail { get; set; }
  }

  [Table("EmployeeDetail")]
  public class EmployeeDetail
  {
    [Key]
    public decimal EmployeeId { get; set; }

    public Employee Employee { get; set; }
  }
}

Here is the stacktrace of error

System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=Microsoft.EntityFrameworkCore
  StackTrace:
   at Microsoft.EntityFrameworkCore.Metadata.Conventions.ForeignKeyAttributeConvention.UpdateRelationshipBuilder(IConventionForeignKeyBuilder relationshipBuilder, IConventionContext context)
   at Microsoft.EntityFrameworkCore.Metadata.Conventions.ForeignKeyAttributeConvention.ProcessForeignKeyAdded(IConventionForeignKeyBuilder relationshipBuilder, IConventionContext`1 context)
   at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.ImmediateConventionScope.OnForeignKeyAdded(IConventionForeignKeyBuilder relationshipBuilder)
   at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.OnForeignKeyAddedNode.Run(ConventionDispatcher dispatcher)
   at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.DelayedConventionScope.Run(ConventionDispatcher dispatcher)
   at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.ConventionBatch.Run()
   at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.ConventionBatch.Run(IConventionForeignKey foreignKey)
   at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionBatchExtensions.Run(IConventionBatch batch, InternalForeignKeyBuilder relationshipBuilder)
   at Microsoft.EntityFrameworkCore.Metadata.Builders.ReferenceReferenceBuilder.HasForeignKeyBuilder(EntityType dependentEntityType, String dependentEntityTypeName, Func`3 hasForeignKey)
   at Microsoft.EntityFrameworkCore.Metadata.Builders.ReferenceReferenceBuilder.HasForeignKeyBuilder(EntityType dependentEntityType, String dependentEntityTypeName, IReadOnlyList`1 foreignKeyMembers)
   at Microsoft.EntityFrameworkCore.Metadata.Builders.ReferenceReferenceBuilder`2.HasForeignKey[TDependentEntity](Expression`1 foreignKeyExpression)
   at EFCoreProject.CompanyContext.OnModelCreating(ModelBuilder modelBuilder) in C:\Users\shivamg\source\repos\ConsoleApp63\ConsoleApp63\Program.cs:line 42

I suppose this is negative scenario since Data Annotation and fluent API used for foreign key are conflicting. But since fluent API takes precedence data annotation, there should not be any error. As it used to work in EFCore 5.
Is this an actual issue or expected behavior.
If this is an issue, when can we expect it to get fixed.
If this is an expected behavior, can we have more meaningful error message instead of null reference exception?
Please could someone clarify.

provider and version information

EF Core version: 6.0.0-rc.2.21480.5
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 6.0
Operating system: Windows 10
IDE: Visual Studio 2022 preview

@ErikEJ
Copy link
Contributor

ErikEJ commented Oct 22, 2021

Have you tried the daily build?

@ajcvickers
Copy link
Member

Note for triage: Still repros on "6.0.0-rtm.21519.8".

@ajcvickers
Copy link
Member

@AndriySvyryd Ping; possible patch.

AndriySvyryd added a commit that referenced this issue Nov 3, 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 Nov 3, 2021
@AndriySvyryd AndriySvyryd removed their assignment Nov 3, 2021
AndriySvyryd added a commit that referenced this issue Nov 3, 2021
@AndriySvyryd AndriySvyryd modified the milestones: 6.0.x, 6.0.1 Nov 9, 2021
@ajcvickers
Copy link
Member

FYI for those impacted by this issue: EF Core 6.0.1 is now available from NuGet.

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 regression Servicing-approved type-bug
Projects
None yet
Development

No branches or pull requests

4 participants