Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems creating a Foreign-Key relationship on Entity Framework

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.

like image 773
user2541621 Avatar asked Jul 02 '13 07:07

user2541621


People also ask

How do I fix foreign key constraint failure?

The error message itself showing there is a foreign key constraint error, which means you are deleting a parent table where the child table contains the Primary table identifier as a foreign key. To avoid this error, you need to delete child table records first and after that the parent table record.

What problems do foreign keys introduce?

Foreign key problems. Many database users encounter foreign key errors, often due to referential integrity problems. A foreign key might point to data that no longer exists, or the foreign key's data type doesn't match the primary key data type, eroding referential integrity.

Could not create foreign key constraint this may have happened because there are?

Could not create foreign key constraint. This may have happened because there are 'UpdatedBy' values of entity 'Suppliers' with no corresponding value in entity 'User', or attribute 'UpdatedBy' of entity 'Suppliers' is creating a circular dependency between entities. Check the Error Log for more information."


1 Answers

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.

like image 141
Slauma Avatar answered Oct 05 '22 00:10

Slauma