C# – Problems creating a Foreign-Key relationship on Entity Framework

cef-code-firstentity-frameworkforeign-keysnet

i'm having trouble configuring a foreign key relationship in my Entity Framework fluent Api:

Here is the head of the report:

 public class Testata
{
    public Testata() { Details = new List<Dettaglio>(); }
    public virtual int IDTEST { get; set; }
    public virtual string Value { get; set; }
    public virtual int IDDETAIL { get; set; }
    public virtual string IDTESTALT { get; set; }
    public virtual byte[] BLOB { get; set; }

    public virtual IList<Dettaglio> Details { get; set; }
}

This is the report's detail

public class Dettaglio
{
    public virtual int IDDETAIL { get; set; }
    public virtual int IDTEST { get; set; }
    public virtual string DSDETAIL { get; set; }

    public virtual Testata TEST_TABLE { get; set; }
}

And this is my fluent API definition of both. Head of the report:

public TEST_TABLEMap()
    {
        // Primary Key
        this.HasKey(t => t.IDTEST)
            .Property(t => t.IDTEST)
            .IsRequired()
            .HasColumnType("Int")
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
            .HasColumnName("IDTEST");


        // Table & Column Mappings
        this.ToTable("TEST_TABLE");
        this.Property(t => t.Value).HasColumnName("DSVALUETEST");
        this.Property(t => t.IDTESTALT).HasColumnName("IDTESTALT");
        this.Property(t => t.BLOB).HasColumnName("BLOB");
    }

Detail of the report:

public TEST_DETAILMap()
    {
        // Primary Key
        this.HasKey(t => t.DSDETAIL);

        // Properties
        this.Property(t => t.DSDETAIL);

        // Table & Column Mappings
        this.ToTable("TEST_DETAIL");
        this.Property(t => t.IDDETAIL).HasColumnName("IDDETAIL");
        // this.Property(t => t.IDTEST).HasColumnName("IDTEST");
        this.Property(t => t.DSDETAIL).HasColumnName("DSDETAIL");

        // Relationships
        this.HasOptional(t => t.TEST_TABLE)
            .WithMany(t => t.Details)
            .HasForeignKey(d => d.IDDETAIL).WillCascadeOnDelete(true);

    }

On execution i always get this error

System.Data.Entity.Edm.EdmAssociationType: : Multiplicity conflicts with the referential constraint in Role 'Dettaglio_TEST_TABLE_Target' in relationship 'Dettaglio_TEST_TABLE'. Because all of the properties in the Dependent Role are non-nullable, multiplicity of the Principal Role must be '1'.

Which, i guess, means i'm failing something at foreign key definition, but i don't really know where to look at.
Any help/hint is much appreciated.

Best Answer

There is a conflict between your foreign key property in class Dettaglio...

public virtual int IDTEST { get; set; }

...which has a non-nullable type (int) and therefore cannot be optional and your mapping...

this.HasOptional(t => t.TEST_TABLE) //...

...where you want the relationship to be optional.

If you indeed want an optional relationship use a nullable FK property:

public virtual int? IDTEST { get; set; }

Otherwise you must use HasRequired for a required relationship with a non-nullable FK property.