Entity Framework Core & Oracle: ignoring nullability of navigation property

42 Views Asked by At

I'm using Entity Framework Core to access an Oracle database and am getting an InvalidCastException saying "Column contains NULL data" on a field that's marked as nullable. It's a navigation property on a one-to-many relationship that can be null (not all parent records have children) but instead of working how the docs say, it's throwing errors when trying to convert this null to an int.

The entities:

[Table("BASES")]
public class Base
{
    public int BaseId {get; set;}
    public int? BaseEntityId {get; set;}    
    public virtual List<AddressLink>? AddressLinks { get; set; } = [];
    // Other properties omitted
}


[Table("ADDR_LINK")]
public class AddressLink
{
    [Key]
    [Column("LINK_ID")]
    public int LinkId { get; set; }
    [Column("ENTITY_ID")]
    public int EntityId { get; set; }
    public Base Base { get; set; }
    // Other properties omitted
}

// And in the context:
modelBuilder.Entity<Base>()
    .HasMany(b => b.AddressLinks)
    .WithOne(l => l.Base)
    .HasForeignKey(l => l.EntityId)
    .HasPrincipalKey(b => b.BaseEntityId);

When selecting a base with no BaseEntityId, all the documentation implies that that base should simply not have a collection of AddressLinks, but instead it's throwing an exception that the "Column contains NULL data".

Edited to add: In a perfect world I'd redo the database structure to point at the Base's primary key but sadly this is the back-end of a piece of commercial software that we license, so I'm not able to make any changes to the database layout.

0

There are 0 best solutions below