使用EF5 Code First我有以下两个类:
public class MicroPost : IEntity
{
private ICollection<Hashtag> hashtags;
private ICollection<MicroPostComment> comment;
private ICollection<FlaggedInappropriate> flag;
public long Id { get; set; }
public int SenderId { get; set; }
public int CreatorId { get; set; }
public string Body { get; set; }
public virtual ICollection<Hashtag> Hashtags
{
get { return hashtags ?? new HashSet<Hashtag>(); }
set { hashtags = value; }
}
public virtual ICollection<MicroPostComment> Comments
{
get { return comment ?? new HashSet<MicroPostComment>(); }
set { comment = value; }
}
public int Like { get; set; }
public bool FlagInappropriate
{
get { return FlagInappropriates.Any(x => x.Approved); }
}
public ICollection<FlaggedInappropriate> FlagInappropriates
{
get { return flag ?? new HashSet<FlaggedInappropriate>(); }
set { flag = value; }
}
public DateTime DateSent { get; set; }
}
public class MicroPostComment : IEntity
{
private ICollection<FlaggedInappropriate> flag;
public long Id { get; set; }
public long? MicropostId { get; set; }
public int SenderId { get; set; }
public string Body { get; set; }
public bool FlagInappropriate
{
get { return FlagInappropriates.Any(x => x.Approved); }
}
public ICollection<FlaggedInappropriate> FlagInappropriates
{
get { return flag ?? new HashSet<FlaggedInappropriate>(); }
set { flag = value; }
}
public DateTime DateSent { get; set; }
}
两个类之间的关系按以下方式配置:
public class MicroPostConfig : EntityTypeConfiguration<MicroPost>, IEntityConfiguration
{
public MicroPostConfig()
{
HasKey(p => p.Id);
Property(p => p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity).HasColumnName("ID");
Property(p => p.Body).HasColumnType("nvarchar").HasMaxLength(50).IsRequired().HasColumnName("BODY");
Property(p => p.SenderId).IsRequired().HasColumnName("SENDER_ID");
Property(p => p.CreatorId).IsRequired().HasColumnName("CREATOR_ID");
Property(p => p.Like).IsRequired().HasColumnName("LIKE");
Property(p => p.DateSent).IsRequired().HasColumnName("DATE_SENT");
Ignore(p => p.FlagInappropriate);
// Navigation properties
HasMany(r => r.Hashtags).WithOptional().HasForeignKey(n => n.MicropostId);
HasMany(r => r.Comments).WithOptional().HasForeignKey(n => n.MicropostId);
HasMany(r => r.FlagInappropriates).WithOptional().HasForeignKey(n => n.PostId).WillCascadeOnDelete(false);
}
/// <summary>
/// Adds the configuration.
/// </summary>
/// <param name="registrar">The registrar.</param>
public void AddConfiguration(ConfigurationRegistrar registrar)
{
registrar.Add(this);
}
}
以下方法通过急切加载评论来检索帖子:
public IEnumerable<MicroPost> GetPostsWithComments(DateTime? fromDate = null, DateTime? toDate = null,int fromIndex = 0, int maxMessages = 20)
{
if (toDate == null)
toDate = DateTime.Now;
IEnumerable<MicroPost> result = fromDate != null
? repository.GetQuery(x => x.DateSent >= fromDate.Value && x.DateSent <= toDate).Include(x => x.Comments)
: repository.GetQuery(x => x.DateSent <= toDate).Include(x => x.Comments);
return result.AsEnumerable();
}
EF5生成以下SQL:
SELECT
[Project1].[SENDER_ID] AS [SENDER_ID],
[Project1].[ID] AS [ID],
[Project1].[CREATOR_ID] AS [CREATOR_ID],
[Project1].[BODY] AS [BODY],
[Project1].[LIKE] AS [LIKE],
[Project1].[DATE_SENT] AS [DATE_SENT],
[Project1].[C1] AS [C1],
[Project1].[ID1] AS [ID1],
[Project1].[MICROPOST_ID] AS [MICROPOST_ID],
[Project1].[SENDER_ID1] AS [SENDER_ID1],
[Project1].[BODY1] AS [BODY1],
[Project1].[DATE_SENT1] AS [DATE_SENT1]
FROM ( SELECT
[Extent1].[ID] AS [ID],
[Extent1].[SENDER_ID] AS [SENDER_ID],
[Extent1].[CREATOR_ID] AS [CREATOR_ID],
[Extent1].[BODY] AS [BODY],
[Extent1].[LIKE] AS [LIKE],
[Extent1].[DATE_SENT] AS [DATE_SENT],
[Extent2].[ID] AS [ID1],
[Extent2].[MICROPOST_ID] AS [MICROPOST_ID],
[Extent2].[SENDER_ID] AS [SENDER_ID1],
[Extent2].[BODY] AS [BODY1],
[Extent2].[DATE_SENT] AS [DATE_SENT1],
CASE WHEN ([Extent2].[SENDER_ID] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1]
FROM [dbo].[MicroPost] AS [Extent1]
LEFT OUTER JOIN [dbo].[MicroPostComment] AS [Extent2] ON [Extent1].[ID] = [Extent2].[MICROPOST_ID]
WHERE CAST( [Extent1].[DATE_SENT] AS datetime2) <= @p__linq__0
) AS [Project1]
ORDER BY [Project1].[ID] ASC, [Project1].[C1] ASC
如果我在db上运行此查询,则会检索包含注释的数据。但是,MicroPost类的属性Comments始终为null。任何人都知道可能出错的地方。我们已经尝试了所有类型的配置选项而没有结果,我们很难理解为什么这不起作用。