I'm trying to map two entities to the same table with the same columns. The differences are HasQueryFilter condition for every entity and a column that takes part in the reference. The entities describe ralationship between dictionary and entity. When I launch it, I receive the following message: "... both mapped to column '' in '' but are configured with different nullability."

public class KeyValuesEntity : BaseEntity
{
    [Column("keyalphanr")]
    [StringLength(255)]
    public string keyalphanumber { get; set; }

    [Column("keydesc")]
    [StringLength(255)]
    public string keydescription { get; set; }

    [Column("keyvalint")]
    public int? keyvalueint { get; set; }
}

[Table("keyval")]
public class KeyValuesBaseEntity : BaseEntity
{
    [Column("keyalphanr")]
    [StringLength(255)]
    public string keyalphanumber { get; set; }

    [Column("keydesc")]
    [StringLength(255)]
    public string keydescription { get; set; }

    [Column("keyvalint")]
    public int? keyvalueint { get; set; }

    protected KeyValuesBaseEntity()
    {

    }
}

public sealed class OptionsEntity : KeyValuesBaseEntity
{

}

var lboptionsBuilder = modelBuilder.Entity<LBOptionsEntity>();

lboptionsBuilder.HasOne(lboptions => lboptions.optionholderKeyValue)
            .WithOne()
            .HasForeignKey<LBOptionsEntity>(lboptions => lboptions.optionholder)
            .HasPrincipalKey<OptionsEntity>(keyval => keyval.keyalphanumber);

Dani AI

Generated

Short answer: EF Core is detecting two properties mapped to the same column but with different nullability. This is a common table‑splitting / shared‑table issue introduced by EF Core 3.x (dependents mapped to the same table are treated as optional and non-key properties are made nullable), so one side ends up nullable while the other is not. See the EF Core breaking‑changes note. (learn.microsoft.com)

How to diagnose quickly

  • Generate a migration (or inspect the model snapshot) and look at the CreateTable column definitions: the migration shows which column EF thinks is nullable.
  • Inspect the runtime model: you can query the model metadata to check a mapped property’s nullability, for example via the model/metadata API (find the entity type, then the property and check IsNullable). This lets you find the two properties that disagree without scanning every class. See the table‑splitting docs for how EF maps multiple types to one table. (learn.microsoft.com)

Fixes and tradeoffs

  • Make the two mapped properties agree: explicitly configure nullability with the Fluent API (.Property(...).IsRequired() or .IsRequired(false)).
  • Or make the dependent navigation required so EF will map the dependent as required (use the navigation API .Navigation(...).IsRequired() when appropriate).
  • If neither is acceptable, map the dependent to its own table (avoid table splitting) or make the shared column nullable on both types. Each approach has behaviour/performance tradeoffs; see the one‑to‑one and table‑splitting docs for guidance. (learn.microsoft.com)

Context & request
This is the scenario described; it often shows up after upgrading to EF Core 3.x (community threads discuss the regression and tracking). If you can, post the full exact exception text and the generated migration CreateTable snippet (as requested) and the EF Core version — that will make it trivial to point to the exact column/property pair and the simplest fix. (stackoverflow.com)

Can you post the complete exact error message please?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.