Note that there are some explanatory texts on larger screens.

plurals
  1. POProblems creating a Foreign-Key relationship on Entity Framework
    text
    copied!<p>i'm having trouble configuring a foreign key relationship in my Entity Framework fluent Api:</p> <p>Here is the head of the report:</p> <pre><code> public class Testata { public Testata() { Details = new List&lt;Dettaglio&gt;(); } 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&lt;Dettaglio&gt; Details { get; set; } } </code></pre> <p>This is the report's detail</p> <pre><code>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; } } </code></pre> <p>And this is my fluent API definition of both. Head of the report:</p> <pre><code>public TEST_TABLEMap() { // Primary Key this.HasKey(t =&gt; t.IDTEST) .Property(t =&gt; t.IDTEST) .IsRequired() .HasColumnType("Int") .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity) .HasColumnName("IDTEST"); // Table &amp; Column Mappings this.ToTable("TEST_TABLE"); this.Property(t =&gt; t.Value).HasColumnName("DSVALUETEST"); this.Property(t =&gt; t.IDTESTALT).HasColumnName("IDTESTALT"); this.Property(t =&gt; t.BLOB).HasColumnName("BLOB"); } </code></pre> <p>Detail of the report:</p> <pre><code>public TEST_DETAILMap() { // Primary Key this.HasKey(t =&gt; t.DSDETAIL); // Properties this.Property(t =&gt; t.DSDETAIL); // Table &amp; Column Mappings this.ToTable("TEST_DETAIL"); this.Property(t =&gt; t.IDDETAIL).HasColumnName("IDDETAIL"); // this.Property(t =&gt; t.IDTEST).HasColumnName("IDTEST"); this.Property(t =&gt; t.DSDETAIL).HasColumnName("DSDETAIL"); // Relationships this.HasOptional(t =&gt; t.TEST_TABLE) .WithMany(t =&gt; t.Details) .HasForeignKey(d =&gt; d.IDDETAIL).WillCascadeOnDelete(true); } </code></pre> <p>On execution i always get this error</p> <blockquote> <p>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'.</p> </blockquote> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload