EF Fluent API many to one relation, navigation property is not fetched

*爱你&永不变心* 提交于 2019-12-11 03:48:55

问题


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.

It is the following relation:

Link to the database diagram

As you can see, I have a Exercise which is related to an ExerciseType. The problem is, the Exercise.ExerciseType navigation property, is not loaded. The relation I made is as follows:

EntityTypeConfiguration<Exercise>
...
this.HasRequired(ex => ex.ExerciseType)
    .WithMany(exType => exType.Exercises)
    .HasForeignKey(ex => ex.ExerciseTypeId);

The problem is that there is no error for me to google on. The entities are fetched, but the related EntityType property on the Exercise objects, is never fetched.

I am using the following query to force the ExerciseType to be fetched, but that doesn't seem to make it work either.

    List<Exercise> exs = db.Exercises.Include(t => t.ExerciseType).ToList();

Is there someting wrong with the relationship I created? Or is there something wrong with the database configuration perhaps?

Code for the entities:

public class ExerciseType
{
    public int ExerciseTypeId { get; set; }
    public string ExerciseTypeName { get; set; }
    public System.DateTime CreatedOn { get; set; }
    public Nullable<int> CreatedBy { get; set; }
    public Nullable<System.DateTime> ModifiedOn { get; set; }
    public Nullable<int> ModifiedBy { get; set; }
    public virtual ICollection<Exercise> Exercises { get; set; }
}

public class Exercise
{
    public Exercise()
    {
        this.ExerciseTemplateMembers = new List<ExerciseTemplateMember>();
        this.TrainingSchemeMembers = new List<TrainingSchemeMember>();
        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<int> CreatedBy { get; set; }
    public Nullable<System.DateTime> ModifiedOn { get; set; }
    public Nullable<int> ModifiedBy { get; set; }
    public virtual ICollection<ExerciseTemplateMember> ExerciseTemplateMembers { get; set; }
    public virtual ICollection<TrainingSchemeMember> TrainingSchemeMembers { get; set; }
    public virtual ExerciseType ExerciseType { get; set; }
}

    public ExerciseMap()
    {
        // Primary Key
        this.HasKey(t => t.ExerciseId);

        // Properties
        this.Property(t => t.ExerciseName)
            .HasMaxLength(50);

        this.Property(t => t.DescriptionHowTo)
            .HasMaxLength(250);

        this.Property(t => t.DescriptionResult)
            .HasMaxLength(250);

        this.Property(t => t.ExerciseVideoUrl)
            .HasMaxLength(200);

        // Table & Column Mappings
        this.ToTable("Exercise");
        this.Property(t => t.ExerciseId).HasColumnName("ExerciseId");
        this.Property(t => t.ExerciseTypeId).HasColumnName("ExerciseTypeId");
        this.Property(t => t.ExerciseName).HasColumnName("ExerciseName");
        this.Property(t => t.DescriptionHowTo).HasColumnName("DescriptionHowTo");
        this.Property(t => t.DescriptionResult).HasColumnName("DescriptionResult");
        this.Property(t => t.ExerciseImage1).HasColumnName("ExerciseImage1");
        this.Property(t => t.ExerciseImage2).HasColumnName("ExerciseImage2");
        this.Property(t => t.ExerciseVideoUrl).HasColumnName("ExerciseVideoUrl");
        this.Property(t => t.Enabled).HasColumnName("Enabled");
        this.Property(t => t.CreatedOn).HasColumnName("CreatedOn");
        this.Property(t => t.CreatedBy).HasColumnName("CreatedBy");
        this.Property(t => t.ModifiedOn).HasColumnName("ModifiedOn");
        this.Property(t => t.ModifiedBy).HasColumnName("ModifiedBy");

        // Relationships
        this.HasRequired(ex => ex.ExerciseType)
            .WithMany(exType => exType.Exercises)
            .HasForeignKey(ex => ex.ExerciseTypeId);
    }

    public ExerciseTypeMap()
    {
        // Primary Key
        this.HasKey(t => t.ExerciseTypeId);

        // Properties
        this.Property(t => t.ExerciseTypeName)
            .IsRequired()
            .HasMaxLength(50);

        // Table & Column Mappings
        this.ToTable("ExerciseType");
        this.Property(t => t.ExerciseTypeId).HasColumnName("ExerciseTypeId");
        this.Property(t => t.ExerciseTypeName).HasColumnName("ExerciseTypeName");
        this.Property(t => t.CreatedOn).HasColumnName("CreatedOn");
        this.Property(t => t.CreatedBy).HasColumnName("CreatedBy");
        this.Property(t => t.ModifiedOn).HasColumnName("ModifiedOn");
        this.Property(t => t.ModifiedBy).HasColumnName("ModifiedBy");
    }

回答1:


Remove this line from the Exercise constructor:

this.ExerciseType = new ExerciseType();

Instantiating navigation reference properties in the default constructor causes all sorts of strange problems, like this for example: What would cause the Entity Framework to save an unloaded (but lazy loadable) reference over existing data? Or this: EF 4.1 Code First: Why is EF not setting this navigation property?




回答2:


Could you please provide code of EntityType class? I had same problem with EF 4.3.1 today when I forgot to make the property (ExerciseType in your case) virtual. If that's not the case, please provide code of the class after all.

If you use lazy loading feature your entities have to have navigation properties marked virtual. EF generates proxy class inheriting from your entity, and if the property is not virtual, the derived class cannot provide it's own implementation of that property (implementation which should load related object/collection lazily).



来源:https://stackoverflow.com/questions/11197546/ef-fluent-api-many-to-one-relation-navigation-property-is-not-fetched

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!