Description
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