我有一个使用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");
}
答案 0 :(得分:2)
从Exercise
构造函数中删除这一行:
this.ExerciseType = new ExerciseType();
在默认构造函数中实例化导航引用属性会导致各种奇怪的问题,例如:What would cause the Entity Framework to save an unloaded (but lazy loadable) reference over existing data?或者:EF 4.1 Code First: Why is EF not setting this navigation property?
答案 1 :(得分:0)
您能否提供EntityType
课程的代码?我今天忘记制作属性(ExerciseType
虚拟)时,我遇到了与EF 4.3.1相同的问题。如果情况并非如此,请提供课程代码。
如果您使用延迟加载功能,则您的实体必须将导航属性标记为虚拟。 EF生成从您的实体继承的代理类,如果该属性不是虚拟的,则派生类不能提供它自己的该属性的实现(实现应该懒惰地加载相关的对象/集合)。