Note that there are some explanatory texts on larger screens.

plurals
  1. POEF Fluent API many to one relation, navigation property is not fetched
    primarykey
    data
    text
    <p>I have a database with the Entity Framework 5 RC (with Fluent API) working now, but I can't seem to get a specific relation to work. And it is driving me nuts for the past few nights I'm working on it.</p> <p>It is the following relation:</p> <p><a href="http://roetje.home.xs4all.nl/static/ExerciseRelation.png" rel="nofollow">Link to the database diagram</a></p> <p>As you can see, I have a <code>Exercise</code> which is related to an <code>ExerciseType</code>. The problem is, the <code>Exercise.ExerciseType</code> navigation property, is not loaded. The relation I made is as follows:</p> <pre><code>EntityTypeConfiguration&lt;Exercise&gt; ... this.HasRequired(ex =&gt; ex.ExerciseType) .WithMany(exType =&gt; exType.Exercises) .HasForeignKey(ex =&gt; ex.ExerciseTypeId); </code></pre> <p>The problem is that there is no error for me to google on. The entities are fetched, but the related <code>EntityType</code> property on the <code>Exercise</code> objects, is never fetched. </p> <p>I am using the following query to force the <code>ExerciseType</code> to be fetched, but that doesn't seem to make it work either.</p> <pre><code> List&lt;Exercise&gt; exs = db.Exercises.Include(t =&gt; t.ExerciseType).ToList(); </code></pre> <p>Is there someting wrong with the relationship I created? Or is there something wrong with the database configuration perhaps? </p> <p>Code for the entities:</p> <pre><code>public class ExerciseType { public int ExerciseTypeId { get; set; } public string ExerciseTypeName { get; set; } public System.DateTime CreatedOn { get; set; } public Nullable&lt;int&gt; CreatedBy { get; set; } public Nullable&lt;System.DateTime&gt; ModifiedOn { get; set; } public Nullable&lt;int&gt; ModifiedBy { get; set; } public virtual ICollection&lt;Exercise&gt; Exercises { get; set; } } public class Exercise { public Exercise() { this.ExerciseTemplateMembers = new List&lt;ExerciseTemplateMember&gt;(); this.TrainingSchemeMembers = new List&lt;TrainingSchemeMember&gt;(); this.ExerciseType = new ExerciseType(); } public int ExerciseId { get; set; } public int ExerciseTypeId { get; set; } public string ExerciseName { get; set; } public string DescriptionHowTo { get; set; } public string DescriptionResult { get; set; } public byte[] ExerciseImage1 { get; set; } public byte[] ExerciseImage2 { get; set; } public string ExerciseVideoUrl { get; set; } public bool Enabled { get; set; } public System.DateTime CreatedOn { get; set; } public Nullable&lt;int&gt; CreatedBy { get; set; } public Nullable&lt;System.DateTime&gt; ModifiedOn { get; set; } public Nullable&lt;int&gt; ModifiedBy { get; set; } public virtual ICollection&lt;ExerciseTemplateMember&gt; ExerciseTemplateMembers { get; set; } public virtual ICollection&lt;TrainingSchemeMember&gt; TrainingSchemeMembers { get; set; } public virtual ExerciseType ExerciseType { get; set; } } public ExerciseMap() { // Primary Key this.HasKey(t =&gt; t.ExerciseId); // Properties this.Property(t =&gt; t.ExerciseName) .HasMaxLength(50); this.Property(t =&gt; t.DescriptionHowTo) .HasMaxLength(250); this.Property(t =&gt; t.DescriptionResult) .HasMaxLength(250); this.Property(t =&gt; t.ExerciseVideoUrl) .HasMaxLength(200); // Table &amp; Column Mappings this.ToTable("Exercise"); this.Property(t =&gt; t.ExerciseId).HasColumnName("ExerciseId"); this.Property(t =&gt; t.ExerciseTypeId).HasColumnName("ExerciseTypeId"); this.Property(t =&gt; t.ExerciseName).HasColumnName("ExerciseName"); this.Property(t =&gt; t.DescriptionHowTo).HasColumnName("DescriptionHowTo"); this.Property(t =&gt; t.DescriptionResult).HasColumnName("DescriptionResult"); this.Property(t =&gt; t.ExerciseImage1).HasColumnName("ExerciseImage1"); this.Property(t =&gt; t.ExerciseImage2).HasColumnName("ExerciseImage2"); this.Property(t =&gt; t.ExerciseVideoUrl).HasColumnName("ExerciseVideoUrl"); this.Property(t =&gt; t.Enabled).HasColumnName("Enabled"); this.Property(t =&gt; t.CreatedOn).HasColumnName("CreatedOn"); this.Property(t =&gt; t.CreatedBy).HasColumnName("CreatedBy"); this.Property(t =&gt; t.ModifiedOn).HasColumnName("ModifiedOn"); this.Property(t =&gt; t.ModifiedBy).HasColumnName("ModifiedBy"); // Relationships this.HasRequired(ex =&gt; ex.ExerciseType) .WithMany(exType =&gt; exType.Exercises) .HasForeignKey(ex =&gt; ex.ExerciseTypeId); } public ExerciseTypeMap() { // Primary Key this.HasKey(t =&gt; t.ExerciseTypeId); // Properties this.Property(t =&gt; t.ExerciseTypeName) .IsRequired() .HasMaxLength(50); // Table &amp; Column Mappings this.ToTable("ExerciseType"); this.Property(t =&gt; t.ExerciseTypeId).HasColumnName("ExerciseTypeId"); this.Property(t =&gt; t.ExerciseTypeName).HasColumnName("ExerciseTypeName"); this.Property(t =&gt; t.CreatedOn).HasColumnName("CreatedOn"); this.Property(t =&gt; t.CreatedBy).HasColumnName("CreatedBy"); this.Property(t =&gt; t.ModifiedOn).HasColumnName("ModifiedOn"); this.Property(t =&gt; t.ModifiedBy).HasColumnName("ModifiedBy"); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

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