我现在有一个使用 Entity Framework 5 RC(使用 Fluent API)的数据库,但我似乎无法获得特定的工作关系。在过去的几个晚上,我正在研究它,这让我发疯。
是以下关系:
如您所见,我有一个Exercise
与ExerciseType
. 问题是,Exercise.ExerciseType
导航属性没有加载。我建立的关系如下:
EntityTypeConfiguration<Exercise>
...
this.HasRequired(ex => ex.ExerciseType)
.WithMany(exType => exType.Exercises)
.HasForeignKey(ex => ex.ExerciseTypeId);
问题是我用谷歌搜索没有错误。实体被提取,但对象的相关EntityType
属性Exercise
永远不会被提取。
我正在使用以下查询来强制ExerciseType
获取 ,但这似乎也无法使其工作。
List<Exercise> exs = db.Exercises.Include(t => t.ExerciseType).ToList();
我创建的关系有问题吗?还是数据库配置有问题?
实体代码:
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");
}