Problems creating a Foreign-Key relationship on Entity Framework

.net c# ef-code-first entity-framework foreign-keys

Question

My Entity Framework Fluent API is having problems defining a foreign key relationship:

The report's heading is as follows:

 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 a detail from the report.

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 how I define both in fluent API terms. Report's heading:

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");
    }

Information from 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);

    }

This error always occurs during execution.

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 suppose, suggests I'm doing something wrong with foreign key definition, but I'm not sure what. I would much appreciate any advice.

1
41
1/19/2017 6:56:35 PM

Popular Answer

Conflict between your foreign key property and what you learned in classDettaglio ...

public virtual int IDTEST { get; set; }

which has a type that is not null (int ) and cannot be optional; as a result, your mapping...

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

You want the connection to be optional in this situation.

Use a nullable FK property if you do want an optional relationship.

public virtual int? IDTEST { get; set; }

If not, you must useHasRequired for an essential connection to a non-nullable FK property.

111
7/2/2013 10:21:22 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow